How to Append or Concatenate Strings in Dart?
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 = "Beginner" ; String gfg2 = "For" ; // Printing concatenated result // by the use of '+' operator print(gfg1 + gfg2 + gfg1); } |
Output:
w3wiki
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 = "Beginner" ; 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:
w3wiki Beginner For Beginner
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( 'Beginner' 'For' 'Beginner' ); // Printing concatenated // result with space print( 'Beginner ' 'For ' 'Beginner' ); } |
Output:
w3wiki Beginner For Beginner
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 = [ "Beginner" , "For" , "Beginner" ]; // 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:
w3wiki Beginner For Beginner