The syntax of String.startsWith() method is:
bool startsWith(Pattern pattern, [int index = 0])
This startsWith() method of String checks whether this string starts with a match of pattern
.
Parameter | Optional/Required | Description |
---|---|---|
pattern | required | the pattern to search for within the string |
index | optional [default value is 0] | if provided, matching will start at this index |
String.startsWith() returns value of type bool
.
In this example,
str
with the value 'Hello, world!'.startsWith()
method to check if it starts with 'Hello'.startsWith()
method returns true
.void main() {
String str = 'Hello, world!';
bool startsWithHello = str.startsWith('Hello');
print('Starts with \'Hello\' in str: $startsWithHello');
}
Starts with 'Hello' in str: true
In this example,
str
with the value 'ABCDEF'.startsWith()
method to check if it starts with 'A'.startsWith()
method returns true
.void main() {
String str = 'ABCDEF';
bool startsWithA = str.startsWith('A');
print('Starts with \'A\' in str: $startsWithA');
}
Starts with 'A' in str: true
In this example,
str
with the value 'Lorem ipsum dolor sit amet'.startsWith()
method to check if it starts with 'Lorem'.startsWith()
method returns true
.void main() {
String str = 'Lorem ipsum dolor sit amet';
bool startsWithLorem = str.startsWith('Lorem');
print('Starts with \'Lorem\' in str: $startsWithLorem');
}
Starts with 'Lorem' in str: true
In this Dart tutorial, we learned about startsWith() method of String: the syntax and few working examples with output and detailed explanation for each example.