Dart String endsWith()
Syntax & Examples
Syntax of String.endsWith()
The syntax of String.endsWith() method is:
bool endsWith(String other) This endsWith() method of String checks whether this string ends with other.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | the string to check if it's at the end of the original string |
Return Type
String.endsWith() returns value of type bool.
✐ Examples
1 Check if string ends with 'world!'
In this example,
- We create a string
strwith the value 'Hello, world!'. - We then use the
endsWith()method to check if it ends with 'world!'. Since the given string ends with 'world!', theendsWith()method returnstrue. - We print the result to standard output.
Dart Program
void main() {
String str = 'Hello, world!';
bool endsWithWorld = str.endsWith('world!');
print('Ends with "world!": $endsWithWorld');
}Output
Ends with "world!": true
2 Check if string ends with 'G'
In this example,
- We create a string
strwith the value 'ABCDEF'. - We then use the
endsWith()method to check if it ends with 'G'. Since the given string does not end with 'G', theendsWith()method returnsfalse. - We print the result to standard output.
Dart Program
void main() {
String str = 'ABCDEF';
bool endsWithG = str.endsWith('G');
print('Ends with "G": $endsWithG');
}Output
Ends with "G": false
3 Check if string ends with 'amet'
In this example,
- We create a string
strwith the value 'Lorem ipsum dolor sit amet'. - We then use the
endsWith()method to check if it ends with 'amet'. Since the given string ends with 'amet!', theendsWith()method returnstrue. - We print the result to standard output.
Dart Program
void main() {
String str = 'Lorem ipsum dolor sit amet';
bool endsWithAmet = str.endsWith('amet');
print('Ends with "amet": $endsWithAmet');
}Output
Ends with "amet": true
Summary
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.