The syntax of String.endsWith() method is:
bool endsWith(String other)
This endsWith() method of String checks whether this string ends with other
.
Parameter | Optional/Required | Description |
---|---|---|
other | required | the string to check if it's at the end of the original string |
String.endsWith() returns value of type bool
.
In this example,
str
with the value 'Hello, world!'.endsWith()
method to check if it ends with 'world!'. Since the given string ends with 'world!', the endsWith()
method returns true
.void main() {
String str = 'Hello, world!';
bool endsWithWorld = str.endsWith('world!');
print('Ends with "world!": $endsWithWorld');
}
Ends with "world!": true
In this example,
str
with the value 'ABCDEF'.endsWith()
method to check if it ends with 'G'. Since the given string does not end with 'G', the endsWith()
method returns false
.void main() {
String str = 'ABCDEF';
bool endsWithG = str.endsWith('G');
print('Ends with "G": $endsWithG');
}
Ends with "G": false
In this example,
str
with the value 'Lorem ipsum dolor sit amet'.endsWith()
method to check if it ends with 'amet'. Since the given string ends with 'amet!', the endsWith()
method returns true
.void main() {
String str = 'Lorem ipsum dolor sit amet';
bool endsWithAmet = str.endsWith('amet');
print('Ends with "amet": $endsWithAmet');
}
Ends with "amet": true
In this Dart tutorial, we learned about endsWith() method of String: the syntax and few working examples with output and detailed explanation for each example.