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
num
with the value 42. - We use the
toString()
method onnum
to convert it to a string. - 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,
- We declare a character variable
letter
with the value 'A'. - We use the
toString()
method onletter
to convert it to a string. - 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,
- We declare a string variable
word
with the value 'Dart'. - We use the
toString()
method onword
to obtain its string representation. - 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.