Dart String isEmpty
Syntax & Examples
Syntax of String.isEmpty
The syntax of String.isEmpty property is:
bool isEmpty
This isEmpty property of String checks whether this string is empty.
Return Type
String.isEmpty returns value of type bool
.
✐ Examples
1 Check if an empty string is empty
In this example,
- We create an empty string
str1
. - We then access the
isEmpty
property ofstr1
to check if it is empty. - Since
str1
is empty, theisEmpty
property returnstrue
. - We print the result to standard output.
Dart Program
void main() {
String str1 = ''; // empty string
bool isEmpty1 = str1.isEmpty;
print('Is str1 empty: $isEmpty1');
}
Output
Is str1 empty: true
2 Check if a non-empty string is empty
In this example,
- We create a non-empty string
str2
with the value 'ABC'. - We then access the
isEmpty
property ofstr2
to check if it is empty. - Since
str2
is not empty, theisEmpty
property returnsfalse
. - We print the result to standard output.
Dart Program
void main() {
String str2 = 'ABC'; // non-empty string
bool isEmpty2 = str2.isEmpty;
print('Is str2 empty: $isEmpty2');
}
Output
Is str2 empty: false
3 Check if an empty string is empty
In this example,
- We create an empty string
str3
. - We then access the
isEmpty
property ofstr3
to check if it is empty. - Since
str3
is empty, theisEmpty
property returnstrue
. - We print the result to standard output.
Dart Program
void main() {
String str3 = ''; // empty string
bool isEmpty3 = str3.isEmpty;
print('Is str3 empty: $isEmpty3');
}
Output
Is str3 empty: true
Summary
In this Dart tutorial, we learned about isEmpty property of String: the syntax and few working examples with output and detailed explanation for each example.