Dart String Length Using the length Property
To find the length of a string in Dart, access the string’s length property. It returns an int containing the number of UTF-16 code units in the string.
For ordinary letters, digits, spaces, and punctuation, this value usually matches the number of visible characters. Some Unicode symbols and emoji can occupy more than one UTF-16 code unit, so they require additional consideration.
Syntax for Getting a Dart String Length
The syntax to get length of string is
String.length
Here, String represents a string object or variable. The length property is read-only, so it can be used to retrieve the length but not to modify it.
A common form is:
int length = stringValue.length;
Find the Length of a Dart String
Find String Length
In this example, we will take a string str, and find its length using String.length property.
Dart Program
void main(){
String str = 'HelloTutorialKart';
//get string length
int len = str.length;
print(len);
}
Output
17
The string contains 17 letters, so str.length returns 17.
H e l l o T u t o r i a l K a r t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Get the Length of an Empty Dart String
An empty string contains no code units. Therefore, its length property returns 0.
Dart Program
void main(){
String str = '';
//get string length
int len = str.length;
print(len);
}
Output
0
When you only need to test whether a string contains no text, Dart also provides the isEmpty and isNotEmpty properties.
Count Spaces in a Dart String Length
Spaces are part of a string and are included in its length. The following string contains four letters, one space, and four more letters.
void main() {
String text = 'Dart Code';
print(text.length);
}
Output
9
If leading or trailing whitespace should not be counted, call trim() before reading the length.
void main() {
String text = ' Dart ';
print(text.length);
print(text.trim().length);
}
Output
8
4
Dart String Length with Emoji and Unicode Text
Dart strings are sequences of UTF-16 code units. Therefore, length does not always represent the number of Unicode code points or the number of characters perceived by a reader.
For example, a basic Latin letter occupies one UTF-16 code unit, while many emoji occupy two or more.
void main() {
String letter = 'A';
String emoji = '😀';
print(letter.length);
print(emoji.length);
print(emoji.runes.length);
}
Output
1
2
1
In this example, emoji.length is 2 because the emoji uses a UTF-16 surrogate pair. Its runes.length is 1 because it represents one Unicode code point.
Some visible characters, including flags, family emoji, and letters combined with accent marks, can consist of multiple Unicode code points. For user-perceived character counting, use the characters package rather than relying only on length or runes.length.
Check a Dart String Before Using Its Length
A non-nullable variable declared as String can use length directly. A nullable variable declared as String? must be checked or handled before accessing the property.
void main() {
String? text;
int length = text?.length ?? 0;
print(length);
}
Output
0
The null-aware operator ?. reads the length only when text is not null. The ?? operator supplies 0 when the value is null.
Common Dart String Length Operations
- Use
text.lengthto get the number of UTF-16 code units. - Use
text.isEmptyto check whether the length is zero. - Use
text.isNotEmptyto check whether the string contains at least one code unit. - Use
text.trim().lengthwhen leading and trailing whitespace should be ignored. - Use
text.runes.lengthto count Unicode code points instead of UTF-16 code units.
Frequently Asked Questions About Dart String Length
What does String.length return in Dart?
It returns an int containing the number of UTF-16 code units in the string.
Does Dart string length include spaces?
Yes. Spaces, tabs, line breaks, and other characters stored in the string contribute to its length.
Why can an emoji have a Dart string length greater than one?
Many emoji are encoded using multiple UTF-16 code units. As a result, the value returned by length can be greater than the number of visible emoji.
How do I check whether a Dart string has length zero?
You can use text.length == 0, but text.isEmpty expresses the intent more clearly.
Summary of Dart String Length
In this Dart Tutorial, we learned how to find the length of a String using the property String.length. We also examined empty strings, whitespace, nullable strings, and the difference between UTF-16 code units, Unicode code points, and visible characters.
TutorialKart.com