Dart List join()
Syntax & Examples


Syntax of List.join()

The syntax of List.join() method is:

 String join([String separator = ""]) 

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

Parameters

ParameterOptional/RequiredDescription
separatoroptional [default value is empty string]the string used to separate each element in the concatenated result

Return Type

List.join() returns value of type String.



✐ Examples

1 Join numbers with a dash separator

In this example,

  1. We create a list named numbers containing the numbers [1, 2, 3, 4, 5].
  2. We then use the join() method with separator '-' to concatenate the numbers into a single string.
  3. The concatenated string is printed to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  String result = numbers.join('-'); // Output: '1-2-3-4-5'
  print(result);
}

Output

1-2-3-4-5

2 Join characters with a comma and space separator

In this example,

  1. We create a list named characters containing the characters ['a', 'b', 'c', 'd', 'e'].
  2. We then use the join() method with separator ', ' to concatenate the characters into a single string.
  3. The concatenated string is printed to standard output.

Dart Program

void main() {
  List<String> characters = ['a', 'b', 'c', 'd', 'e'];
  String result = characters.join(', '); // Output: 'a, b, c, d, e'
  print(result);
}

Output

a, b, c, d, e

3 Join strings with a pipe separator

In this example,

  1. We create a list named strings containing the strings ['apple', 'banana', 'cherry'].
  2. We then use the join() method with separator ' | ' to concatenate the strings into a single string.
  3. The concatenated string is printed to standard output.

Dart Program

void main() {
  List<String> strings = ['apple', 'banana', 'cherry'];
  String result = strings.join(' | '); // Output: 'apple | banana | cherry'
  print(result);
}

Output

apple | banana | cherry

Summary

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