The syntax of String.toString() method is:
String toString()
This toString() method of String returns a string representation of this object.
String.toString() returns value of type String
.
In this example,
num
with the value 42.toString()
method on num
to convert it to a string.num
to standard output.void main() {
int num = 42;
String str1 = num.toString();
print('String representation of num: $str1');
}
String representation of num: 42
In this example,
letter
with the value 'A'.toString()
method on letter
to convert it to a string.letter
to standard output.void main() {
char letter = 'A';
String str2 = letter.toString();
print('String representation of letter: $str2');
}
String representation of letter: A
In this example,
word
with the value 'Dart'.toString()
method on word
to obtain its string representation.word
to standard output.void main() {
String word = 'Dart';
String str3 = word.toString();
print('String representation of word: $str3');
}
String representation of word: Dart
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.