In Dart concatenation of string can be done in four different ways:
- By using ‘+’ operator.
- By string interpolation.
- By directly writing string literals.
- 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.