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,
- We declare an integer variable
numwith the value 42. - We use the
toString()method onnumto convert it to a string. - We print the string representation of
numto 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,
- We declare a character variable
letterwith the value 'A'. - We use the
toString()method onletterto convert it to a string. - We print the string representation of
letterto 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,
- We declare a string variable
wordwith the value 'Dart'. - We use the
toString()method onwordto obtain its string representation. - We print the string representation of
wordto 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.