Dart Set toString()
Syntax & Examples
Set.toString() method
The `toString` method in Dart returns a string representation of some or all elements of the set.
Syntax of Set.toString()
The syntax of Set.toString() method is:
String toString() This toString() method of Set returns a string representation of (some of) the elements of this.
Return Type
Set.toString() returns value of type String.
✐ Examples
1 Convert Set of integers to string
In this example,
- We create a Set
numberswith elements 1, 2, and 3. - We call the
toString()method on the set. - The method returns a string representation of the set's elements.
- We print the result to standard output.
Dart Program
void main() {
Set<int> numbers = {1, 2, 3};
var result = numbers.toString();
print('Result: $result');
}Output
Result: {1, 2, 3}2 Convert Set of strings to string
In this example,
- We create a Set
wordswith elements 'apple', 'banana', and 'cherry'. - We call the
toString()method on the set. - The method returns a string representation of the set's elements.
- We print the result to standard output.
Dart Program
void main() {
Set<String> words = {'apple', 'banana', 'cherry'};
var result = words.toString();
print('Result: $result');
}Output
Result: {apple, banana, cherry}Summary
In this Dart tutorial, we learned about toString() method of Set: the syntax and few working examples with output and detailed explanation for each example.