Dart String toString()
Syntax & Examples


Syntax of toString()

The syntax of String.toString() method is:

 String toString() 

This toString() method of String returns a string representation of this object.

Return Type

String.toString() returns value of type String.



✐ Examples

1 Convert number to string

In this example,

  1. We declare an integer variable num with the value 42.
  2. We use the toString() method on num to convert it to a string.
  3. We print the string representation of num to standard output.

Dart Program

void main() {
  int num = 42;
  String str1 = num.toString();
  print('String representation of num: $str1');
}

Output

String representation of num: 42

2 Convert character to string

In this example,

  1. We declare a character variable letter with the value 'A'.
  2. We use the toString() method on letter to convert it to a string.
  3. We print the string representation of letter to standard output.

Dart Program

void main() {
  char letter = 'A';
  String str2 = letter.toString();
  print('String representation of letter: $str2');
}

Output

String representation of letter: A

3 Convert word to string

In this example,

  1. We declare a string variable word with the value 'Dart'.
  2. We use the toString() method on word to obtain its string representation.
  3. We print the string representation of word to standard output.

Dart Program

void main() {
  String word = 'Dart';
  String str3 = word.toString();
  print('String representation of word: $str3');
}

Output

String representation of word: Dart

Summary

In this Dart tutorial, we learned about toString() method of String: the syntax and few working examples with output and detailed explanation for each example.