Dart – Check if String starts with specific other String
To check if a string starts with specific other string in Dart, call startsWith() method on this string and pass the other string as argument.
startsWith() method returns returns a boolean value of true
if this string starts with the other string, or false
if the this string does not start with other string.
Syntax
The syntax to call startsWith() method on this string object str
and pass the other string other
as argument is
</>
Copy
str.startsWith(other)
Example
In this example, we take two strings: str1
and str2
; and check if the string str1
starts with the other string str2
.
main.dart
</>
Copy
void main() {
var str1 = 'Hello World!';
var str2 = 'Hell';
if (str1.startsWith(str2)) {
print('This string starts with other string.');
} else {
print('This string does not start with other string.');
}
}
Output
This string starts with other string.
Conclusion
In this Dart Tutorial, we learned how to check if a string starts with specific other string, using startsWith()
method of String class, with examples.