Dart String Methods
Dart String Methods
A method is an action that belongs to a value.
Strings have many built-in methods, and you call them with a dot after the string.
A string method never changes the original string.
It returns a new value instead:
Example
void main() {
var txt = 'Hello World';
print(txt.toUpperCase());
print(txt.toLowerCase());
print(txt);
}
Try it Yourself »
Result:
HELLO WORLD
hello world
Hello World
Search in a String
contains(), startsWith() and endsWith() return true or false:
Example
void main() {
var email = 'liam@example.com';
print(email.contains('@'));
print(email.startsWith('liam'));
print(email.endsWith('.org'));
}
Try it Yourself »
Result:
true
true
false
indexOf() returns the position of the first match, and -1 if there is no match:
Example
void main() {
var txt = 'I like apples and bananas';
print(txt.indexOf('apples'));
print(txt.indexOf('a'));
print(txt.lastIndexOf('a'));
print(txt.indexOf('cherries'));
}
Try it Yourself »
Result:
7
7
23
-1
Note: All search methods are case-sensitive, so 'Dart'.contains('dart') is false.
Get Part of a String
substring(start, end) returns the characters from index start up to, but not including, index end.
If you leave out end, you get the rest of the string:
Example
void main() {
var txt = 'Hello World';
print(txt.substring(0, 5));
print(txt.substring(6));
}
Try it Yourself »
Result:
Hello
World
Replace, Split and Trim
replaceAll() replaces every match with another string:
Example
void main() {
var txt = 'I like cats. Cats are cute.';
print(txt.replaceAll('cats', 'dogs'));
}
Try it Yourself »
Result:
I like dogs. Cats are cute.
The word Cats was not replaced, because it starts with a capital letter.
split() cuts a string into a list of strings, at every separator:
Example
void main() {
var fruits = 'apple,banana,cherry';
print(fruits.split(','));
}
Try it Yourself »
Result:
[apple, banana, cherry]
trim() removes spaces at the start and at the end of a string, which is useful for text that a user typed:
Example
void main() {
var input = ' Liam ';
print('[' + input + ']');
print('[' + input.trim() + ']');
}
Try it Yourself »
Result:
[ Liam ]
[Liam]
Pad a String
padLeft() adds characters at the start until the string has the length you ask for:
Example
void main() {
var orderId = '42';
print(orderId.padLeft(5, '0'));
print('Tea'.padRight(8, '.') + '3.50');
print('Coffee'.padRight(8, '.') + '4.20');
}
Try it Yourself »
Result:
00042
Tea.....3.50
Coffee..4.20
String Methods Reference
This table lists common properties and methods of Dart strings.
In the examples, s is the string 'Hello World'.
| Property or method | Description | Example | Result |
|---|---|---|---|
length | Number of characters | s.length | 11 |
isEmpty | true if the string has no characters | ''.isEmpty | true |
isNotEmpty | true if the string has at least one character | s.isNotEmpty | true |
toUpperCase() | Returns the string in upper case | s.toUpperCase() | HELLO WORLD |
toLowerCase() | Returns the string in lower case | s.toLowerCase() | hello world |
contains() | Checks if the string contains a value | s.contains('World') | true |
startsWith() | Checks if the string starts with a value | s.startsWith('Hell') | true |
endsWith() | Checks if the string ends with a value | s.endsWith('d') | true |
indexOf() | Position of the first match, or -1 | s.indexOf('o') | 4 |
lastIndexOf() | Position of the last match, or -1 | s.lastIndexOf('o') | 7 |
substring() | Returns part of the string | s.substring(0, 5) | Hello |
replaceAll() | Replaces every match | s.replaceAll('o', '0') | Hell0 W0rld |
replaceFirst() | Replaces the first match only | s.replaceFirst('o', '0') | Hell0 World |
split() | Cuts the string into a list | s.split(' ') | [Hello, World] |
trim() | Removes spaces at both ends | ' Hi '.trim() | Hi |
trimLeft() | Removes spaces at the start | ' Hi'.trimLeft() | Hi |
trimRight() | Removes spaces at the end | 'Hi '.trimRight() | Hi |
padLeft() | Adds characters at the start | '7'.padLeft(3, '0') | 007 |
padRight() | Adds characters at the end | '7'.padRight(3, '0') | 700 |
compareTo() | Compares two strings in alphabetical order: a negative number, 0 or a positive number | 'a'.compareTo('b') | -1 |
* | Repeats the string | 'ha' * 3 | hahaha |