Dart String length
Syntax & Examples
Syntax of String.length
The syntax of String.length property is:
int length This length property of String returns the length of the string.
Return Type
String.length returns value of type int.
✐ Examples
1 Calculate length of 'Hello, world!'
In this example,
- We create a string
str1with the value 'Hello, world!'. - We then access the
lengthproperty ofstr1to get its length. - The length of
str1is printed to standard output.
Dart Program
void main() {
String str1 = 'Hello, world!';
int length1 = str1.length;
print('Length of str1: $length1');
}Output
Length of str1: 13
2 Calculate length of 'ABCDEF'
In this example,
- We create a string
str2with the value 'ABCDEF'. - We then access the
lengthproperty ofstr2to get its length. - The length of
str2is printed to standard output.
Dart Program
void main() {
String str2 = 'ABCDEF';
int length2 = str2.length;
print('Length of str2: $length2');
}Output
Length of str2: 6
3 Calculate length of 'Lorem ipsum dolor sit amet'
In this example,
- We create a string
str3with the value 'Lorem ipsum dolor sit amet'. - We then access the
lengthproperty ofstr3to get its length. - The length of
str3is printed to standard output.
Dart Program
void main() {
String str3 = 'Lorem ipsum dolor sit amet';
int length3 = str3.length;
print('Length of str3: $length3');
}Output
Length of str3: 27
Summary
In this Dart tutorial, we learned about length property of String: the syntax and few working examples with output and detailed explanation for each example.