endsWithIgnoreCase
is a static method of the StringUtils
class that checks if the given string ends with the given string/suffix. This method is case-insensitive when comparing the suffix with the end of the string.
Some special cases to look out for are:
true
when the input string and the suffix are null
. Two null
references are considered to be equal.false
when the input string is null
or the suffix is null
.Note: For case-sensitive comparison, refer StringUtils.endsWith
StringUtils
StringUtils
is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml
file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Note: For other versions of the commons-lang package, refer to the Maven Repository.
You can import the StringUtils
class as follows:
import org.apache.commons.lang3.StringUtils;
public static boolean endsWithIgnoreCase(final CharSequence str, final CharSequence suffix)
The function takes in two parameters:
final CharSequence str
: Character sequence to check.final CharSequence suffix
: the suffix to find in the stringThe function returns true
if the string ends with the suffix case insensitive. Otherwise, it returns false
.
import org.apache.commons.lang3.StringUtils;public class Main{public static void main(String[] args){String string = "educative";String suffix = "TIVE";System.out.println(StringUtils.endsWithIgnoreCase(string, suffix));suffix = "TiVe";System.out.println(StringUtils.endsWithIgnoreCase(string, suffix));System.out.println(StringUtils.endsWithIgnoreCase(string, null));System.out.println(StringUtils.endsWithIgnoreCase(null, null));}}
"educative"
"TIVE"
The function returns true
since the string ends with the suffix in a case-insensitive manner.
"educative"
"TiVe"
The function returns true
since the string ends with the suffix in a case-insensitive manner.
"educative"
null
The function returns false
since the suffix is null
.
null
null
The function returns true
since the string and the suffix are null
.
The output of the code will be as follows:
true
true
false
true