Dart Set join()
Syntax & Examples


Set.join() method

The `join` method in Dart converts each element of the set to a String and concatenates the strings using the provided separator.


Syntax of Set.join()

The syntax of Set.join() method is:

 String join([String separator = "" ]) 

This join() method of Set converts each element to a String and concatenates the strings.

Parameters

ParameterOptional/RequiredDescription
separatoroptionalThe string to use as a separator between each element when concatenating. If not provided, an empty string is used as the default separator.

Return Type

Set.join() returns value of type String.



✐ Examples

1 Join integers with comma separator

In this example,

  1. We create a Set set containing integers.
  2. We use the join() method with a comma and space separator to concatenate the elements into a single string.
  3. We print the joined string to standard output.

Dart Program

void main() {
  Set<int> set = {1, 2, 3, 4, 5};
  String result = set.join(', ');
  print('Joined set elements: $result');
}

Output

Joined set elements: 1, 2, 3, 4, 5

2 Join strings with pipe separator

In this example,

  1. We create a Set set containing strings.
  2. We use the join() method with a pipe separator to concatenate the elements into a single string.
  3. We print the joined string to standard output.

Dart Program

void main() {
  Set<String> set = {'apple', 'banana', 'orange'};
  String result = set.join(' | ');
  print('Joined set elements: $result');
}

Output

Joined set elements: apple | banana | orange

Summary

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