Dart .length
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:
1719
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
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Dart on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours