.length

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Apr 19, 2024
Contribute to Docs

In Dart, the .length property returns the number of characters within a given string, including emojis and special symbols. The characters are measured in UTF-16 code units.

Note: The UTF-16 code units refer to the number of 16-bit values used to encode the characters in the string.

Syntax

string.length;
  • string: The name of the string to be checked.

Examples

The following example demonstrates the use of the .length property:

void main() {
String firstString = "I love Codecademy";
String secondString = "I love Codecademy😍";
int lengthOfFirstString = firstString.length;
int lengthOfSecondString = secondString.length;
print(lengthOfFirstString);
print(lengthOfSecondString);
}

The above code produces the following output:

17
19

Note: The emoji 😍 takes 2 UTF-16 code units.

The following example computes the length of an empty string:

void main() {
String emptyString = "";
int lengthOfEmptyString = emptyString.length;
print(lengthOfEmptyString);
}

The output for the above code is as follows:

0

All contributors

Looking to contribute?

Learn Dart on Codecademy