Skip to content
Related Articles

Related Articles

How to Append or Concatenate Strings in Dart?
  • Last Updated : 21 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

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 concated 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 concated result
  // by the use of string
  // interpolation without space
  print('$gfg1$gfg2$gfg1');
    
  // Printing concated 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 concated
  // result without space
  print('Geeks''For''Geeks');
    
  // Printing concated
  // 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
  // concated string
  print(geek1);
    
  // Joining all the elements
  // of the list and converting
  // it to String
  String geek2 = gfg.join(" ");
    
  // Printing the
  // concated string
  print(geek2);
}

 

Output:

GeeksForGeeks
Geeks For Geeks

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :