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
str1
with the value 'Hello, world!'. - We then access the
length
property ofstr1
to get its length. - The length of
str1
is 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
str2
with the value 'ABCDEF'. - We then access the
length
property ofstr2
to get its length. - The length of
str2
is 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
str3
with the value 'Lorem ipsum dolor sit amet'. - We then access the
length
property ofstr3
to get its length. - The length of
str3
is 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.