Skip to content
Related Articles

Related Articles

How to Append or Concatenate Strings in Dart?

Improve Article
Save Article
  • Last Updated : 18 Apr, 2022
Improve Article
Save Article

In Dart concatenation of string can be done in four different ways:

  1. By using ‘+’ operator.
  2. By string interpolation.
  3. By directly writing string literals.
  4. By Strings.join() method.

By ‘ + ‘ Operator

In Dart, we can use the ‘+’ operator to concatenate the strings.

Example: Using ‘+’ operator to concatenate strings in Dart.

Dart




// Main function
void main() {
   
  // Assigning values
  // to the variable
  String gfg1 = "Geeks";
  String gfg2 = "For";
   
  // Printing concatenated result
  // by the use of '+' operator
  print(gfg1 + gfg2 + gfg1);
}

 

Output:

GeeksForGeeks

By String Interpolation

In Dart, we can use string interpolation to concatenate the strings.

Example: Using string interpolation to concatenate strings in Dart. 

Dart




// Main function
void main() {
   
  // Assigning values to the variable
  String gfg1 = "Geeks";
  String gfg2 = "For";
   
  // Printing concatenated result
  // by the use of string
  // interpolation without space
  print('$gfg1$gfg2$gfg1');
   
  // Printing concatenated result
  // by the use of
  // string interpolation
  // with space
  print('$gfg1 $gfg2 $gfg1');
}

 

Output:

GeeksForGeeks
Geeks For Geeks

By Directly Writing String Literals

In Dart, we concatenate the strings by directly writing string literals and appending it.

Example: By directly writing string literals to concatenate strings in Dart. 

Dart




// Main function
void main() {
   
  // Printing concatenated
  // result without space
  print('Geeks''For''Geeks');
   
  // Printing concatenated
  // result with space
  print('Geeks ''For ''Geeks');
}

 

Output:

GeeksForGeeks
Geeks For Geeks

By List_name.join() Method

In Dart, we can also convert the elements of the list into one string.

list_name.join("input_string(s)");

This input_string(s) are inserted between each element of the list in the output string.

Example: Using list_name.join() concatenate strings in Dart. 

Dart




// Main function
void main() { 
   
  // Creating List of strings
  List<String> gfg = ["Geeks", "For" , "Geeks"];
   
  // Joining all the elements
  // of the list and
  // converting it to String
  String geek1 = gfg.join();
   
  // Printing the
  // concatenated string
  print(geek1);
   
  // Joining all the elements
  // of the list and converting
  // it to String
  String geek2 = gfg.join(" ");
   
  // Printing the
  // concatenated string
  print(geek2);
}

 

Output:

GeeksForGeeks
Geeks For Geeks

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!