This Java example shows how to check if string ends with another string using the endsWith
method, using the regular expression or by doing case insensitive comparison.
How to check if a string ends with another string in Java?
You can use the endsWith
method of the String class to check.
1 |
public boolean endsWith(String str) |
The endsWIth
method returns true if the string ends with the argument string,empty false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javacodeexamples.stringexamples; public class StringEndsWithExample { public static void main(String[] args) { String str = "Hello World"; boolean doesEndWith = str.endsWith("World"); System.out.println(str + " ends with World: " + doesEndWith); } } |
Output
1 |
Hello World ends with World: true |
Note:
The endsWith
method always returns true for empty string or if the string is equal to the argument string. For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.stringexamples; public class StringEndsWithExample { public static void main(String[] args) { String str = "Hello World"; System.out.println(str.endsWith("")); System.out.println(str.endsWith(str)); } } |
Output
1 2 |
true true |
How to check if the string ends with any of the given strings?
1) Using the OR operator
Suppose you want to check if the string ends with any of the given multiple string values. Since the endsWith
method accepts only one argument, you need to check it multiple times using OR (||) operator as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javacodeexamples.stringexamples; public class StringEndsWithExample { public static void main(String[] args) { String strFileName = "demo.txt"; if(strFileName.endsWith(".txt") || strFileName.endsWith(".rtf")){ System.out.println("Text File"); }else{ System.out.println("Not a text file"); } } } |
Output
1 |
Text File |
2) Using the Regular Expression
You can also use a regular expression to check the same as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javacodeexamples.stringexamples; public class StringEndsWithExample { public static void main(String[] args) { String strFileName = "demo.txt"; if(strFileName.matches("^.*(\\.txt|\\.rtf)$")){ System.out.println("Text File"); }else{ System.out.println("Not a text file"); } } } |
Output
1 |
Text File |
We have used the "^.*(\\.txt|\\.rtf)$"
pattern to check if the file name ends with “.txt” or “.rtf” file extensions, where
1 2 3 4 |
^ - start of the string .* - any characters (\\.txt|\\.rtf) - followed by .txt or .rtf $ - followed by end of the string |
A dot (.) has a special meaning in the regular expression (it matches any character). Hence, in order to do a literal search of a dot in a string, we need to escape it like “\\.”.
3) Using the Apache Commons library
If you are using the Apache Commons library, you can use the endsWithAny
method of the StringUtils class to check if the string ends with any of the given multiple strings as given below.
1 2 3 4 5 6 7 |
String strFileName = "demo.txt"; if( StringUtils.endsWithAny(strFileName, new String[]{".txt", ".rtf"}) ){ System.out.println("Text File"); }else{ System.out.println("Not a text file"); } |
Output
1 |
Text File |
How to check using case insensitive comparison?
By default, the endsWith
method is case sensitive. For example,
1 |
System.out.println( "demo.txt".endsWith("Txt") ); |
will return “false”.
You can use the toLowerCase
(or the toUpperCase
) method to change the case of the both the strings as given below.
1 2 3 4 5 6 |
String strFileName = "demo.txt"; boolean isTextFile = strFileName.toLowerCase().endsWith( ".txt" ) || strFileName.toLowerCase().endsWith( ".rtf" ); System.out.println(isTextFile); |
Output
1 |
true |
If you are using the Apache Commons library, you can use the endsWithIgnoreCase
method of the StringUtils class to do case insensitive comparison as given below.
1 2 |
String strFileName = "demo.txt"; System.out.println( StringUtils.endsWithIgnoreCase(strFileName, ".TXT") ); |
Output
1 |
true |
This example is a part of the String in Java tutorial.
Please let me know your views in the comments section below.