Append or Concatenate Two Strings in Dart
String concatenation combines two or more strings into one string. In Dart, you can concatenate strings with the + operator, place adjacent string literals next to each other, or use string interpolation when values need to be inserted into text.
The most direct way to append one string to another is:
String result = firstString + secondString;
The + operator accepts strings as operands and returns a new string containing their combined characters. Dart strings are immutable, so the original strings are not modified.
Concatenate Two Dart Strings with the + Operator
In this example, we will take two Dart Strings and concatenate them using +.
Dart Program
void main(){
String str1 = 'Tutorial';
String str2 = 'Kart';
//concatenate str1 and str2
String result = str1 + str2;
print(result);
}
Output
TutorialKart
No separator is inserted automatically. Because str1 does not end with a space and str2 does not begin with one, the result is TutorialKart.
Add Spaces Between Concatenated Strings in Dart
Include a space explicitly when the combined strings represent separate words. The space can be added to either string or supplied as another string operand.
void main() {
String firstName = 'Alex';
String lastName = 'Morgan';
String fullName = firstName + ' ' + lastName;
print(fullName);
}
Output
Alex Morgan
Concatenate More Than Two Strings in Dart
In this example, we will take three Dart Strings and concatenate them using +. We can chain the + operator. Hence, we can concatenate more than two strings in a single statement. Please observe the below example.
Dart Program
void main(){
String str1 = 'Welcome to ';
String str2 = 'Tutorial';
String str3 = 'Kart';
//concatenate str1, str2 and str3
String result = str1 + str2 + str3;
print(result);
}
Output
Welcome to TutorialKart
Dart evaluates this expression from left to right. Each + operation creates a combined string that is used by the next operation.
Join Adjacent String Literals Without the + Operator
Dart automatically combines adjacent string literals. This is useful when a long fixed message is split across multiple lines in the source code.
void main() {
String message = 'Dart supports '
'adjacent string literals.';
print(message);
}
Output
Dart supports adjacent string literals.
This form works only with string literals written directly in the code. Variables still require the + operator, interpolation, or another string-building method.
Concatenate Dart Strings with String Interpolation
String interpolation is usually easier to read when a message contains variables. Use $variableName for a simple variable or ${expression} for an expression.
void main() {
String language = 'Dart';
String topic = 'string concatenation';
String message = 'Learn $topic in $language.';
print(message);
}
Output
Learn string concatenation in Dart.
Braces are needed when accessing a property, performing a calculation, or separating a variable name from following text.
void main() {
String item = 'book';
int quantity = 3;
String message = 'Order: $quantity ${item}s';
print(message);
}
Output
Order: 3 books
Concatenate a Dart String with an int
You can concatenate String with other type of objects. All you need to do is convert the other type of Dart object to String using object.toString() method.
In this example, we will take a String and an int and concatenate them using +. During concatenation, we convert the int to string.
Dart Program
void main(){
String str1 = 'Welcome to ';
int n = 24;
//concatenate str1 and n
String result = str1 + n.toString();
print(result);
}
Output
Welcome to 24
String interpolation provides a shorter alternative because Dart converts the interpolated value to its string representation automatically.
void main() {
int lessons = 24;
String result = 'Welcome to lesson $lessons';
print(result);
}
Output
Welcome to lesson 24
Build a Dart String Repeatedly with StringBuffer
For a small number of values, the + operator and interpolation are straightforward. When a string is assembled repeatedly inside a loop, StringBuffer provides a dedicated way to collect text and create the final string once.
void main() {
var buffer = StringBuffer();
for (int i = 1; i <= 3; i++) {
buffer.write('Item $i');
if (i < 3) {
buffer.write(', ');
}
}
String result = buffer.toString();
print(result);
}
Output
Item 1, Item 2, Item 3
The write() method appends text without inserting a line break. Use writeln() when each appended value should end with a newline.
Join a List of Dart Strings with a Separator
When the values are already stored in a list, use the list’s join() method. The argument passed to join() is inserted between consecutive elements.
void main() {
List<String> words = ['Learn', 'Dart', 'Programming'];
String sentence = words.join(' ');
print(sentence);
}
Output
Learn Dart Programming
Choose the Appropriate Dart String Concatenation Method
| Requirement | Recommended approach |
|---|---|
| Combine two or three string variables | Use the + operator |
| Insert variables or expressions into readable text | Use string interpolation |
| Split a fixed string literal across source lines | Use adjacent string literals |
| Combine list elements with a delimiter | Use join() |
| Append text repeatedly, especially in a loop | Use StringBuffer |
Common Dart String Concatenation Mistakes
- Missing spaces: Dart does not add spaces between concatenated strings. Add
' 'where needed. - Combining a String and int directly: Convert the number with
toString()when using+, or use interpolation. - Forgetting interpolation braces: Use
${...}for expressions, property access, and cases where the variable name must be clearly separated from adjacent text. - Expecting the original string to change: Concatenation returns a new string because Dart strings are immutable. Assign the result to a variable.
- Using repeated + operations in a large loop: Use
StringBufferwhen text is appended many times.
Frequently Asked Questions About Dart String Concatenation
How do I concatenate two strings in Dart?
Place the + operator between the strings, as in String result = first + second;. Add a separate space string when the values should be displayed as separate words.
Can I concatenate a String and an int in Dart?
Yes. Convert the integer with toString() before using +, or insert it with string interpolation, such as 'Count: $count'.
What is the difference between + and string interpolation in Dart?
The + operator explicitly combines string operands. Interpolation inserts variables or expressions inside a string literal and is often easier to read when constructing sentences.
How do I concatenate strings with a newline in Dart?
Insert the newline escape sequence \n between the values, use a multiline string, or call writeln() on a StringBuffer.
When should I use StringBuffer instead of + in Dart?
Use StringBuffer when text is appended repeatedly, such as during a loop. For a small, fixed number of strings, + or interpolation is usually simpler.
Summary of Dart String Concatenation
In this Dart Tutorial, we learned how to append or concatenate two ore more strings.
Use + for direct concatenation, interpolation for readable strings containing values, join() for lists, and StringBuffer for repeated appends. Remember to add spaces, punctuation, and newline characters explicitly where the final text requires them.
TutorialKart.com