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