Dart Strings
Dart Strings
A string is a piece of text, like a name, a sentence or an address.
In Dart, you write strings inside single quotes or double quotes:
Example
void main() {
String a = 'Hello';
String b = "Hello";
print(a);
print(b);
}
Try it Yourself »
Result:
Hello
Hello
Both styles give the same string.
Most Dart code uses single quotes.
Quotes Inside a String
You can use single quotes inside a string with double quotes, and double quotes inside a string with single quotes:
Result:
It's a sunny day
She said "Hello"
Escape Characters
You can also put a backslash \ before a quote, like 'It\'s', so Dart does not read it as the end of the string.
A backslash followed by a letter or a sign is called an escape character.
It puts a special character in the string:
| Code | Result |
|---|---|
\' | Single quote |
\" | Double quote |
\\ | Backslash |
\n | New line |
\t | Tab |
\$ | Dollar sign |
Example
void main() {
print('It\'s a sunny day');
print('Line one\nLine two');
print('Name:\tLiam');
print('C:\\Users\\Liam');
print('Price: \$20');
}
Try it Yourself »
Result:
It's a sunny day
Line one
Line two
Name: Liam
C:\Users\Liam
Price: $20
Multi-line Strings
To write a string over several lines, use three single quotes ''' or three double quotes """:
Example
void main() {
var poem = '''Roses are red,
Violets are blue,
Dart is fun,
And so are you.''';
print(poem);
}
Try it Yourself »
Result:
Roses are red,
Violets are blue,
Dart is fun,
And so are you.
Raw Strings
If you write r before a string, it becomes a raw string.
In a raw string, a backslash is just a backslash, and escape characters do not work:
Result:
Hello\nWorld
Hello
World
Without r, Dart reads \n in the second line as a new line.
Join Strings
You can join two or more strings with the + operator.
This is called concatenation:
Example
void main() {
var firstName = 'John';
var lastName = 'Doe';
var fullName = firstName + ' ' + lastName;
print(fullName);
}
Try it Yourself »
Result:
John Doe
Use += to add text to the end of a string variable.
message += ' morning' is a short way to write message = message + ' morning':
Example
void main() {
var message = 'Good';
message += ' morning';
print(message);
}
Try it Yourself »
Result:
Good morning
Tip: In Dart, it is more common to join strings with string interpolation, which you will learn in the next chapter.
String Length and Characters
The length property gives the number of characters in a string:
Example
void main() {
var txt = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
print(txt.length);
}
Try it Yourself »
Result:
26
You can get one character with its index (its position number) inside square brackets [].
The first character has index 0, the second has index 1, and so on:
Example
void main() {
var fruit = 'Banana';
print(fruit[0]);
print(fruit[2]);
print(fruit[fruit.length - 1]);
}
Try it Yourself »
Result:
B
n
a
Strings in Dart cannot be changed after they are created, so you cannot replace a character like this:
Result:
main.dart:3:8: Error: The operator '[]=' isn't defined for the type 'String'.
Try correcting the operator to an existing operator, or defining a '[]=' operator.
fruit[0] = 'C';
^
Instead, you create a new string, for example with the string methods.