The syntax of String.compareTo() method is:
int compareTo(String other)
This compareTo() method of String compares this string to other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | the pattern to search for within the string |
String.compareTo() returns value of type int
.
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 compareTo() method of String: the syntax and few working examples with output and detailed explanation for each example.