In Dart, the concatenation of strings can be done in four different ways:
- Using the '+' operator.
- Using string interpolation.
- By directly writing string literals.
- Using the .join() method on a list of strings.
1. Using the ' + ' Operator
In Dart, we can use the '+' operator to concatenate the strings.
Example:
// 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:
GeeksForGeeks2. By String Interpolation
In Dart, we can use string interpolation to concatenate the strings.
Example:
// 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
3. By Directly Writing String Literals
In Dart, we concatenate the strings by directly writing string literals and appending.
Example:
// 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
4. 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) is inserted between each element of the list in the output string.
Example:
// 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