Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
stringutils

What is StringUtils.endsWithIgnoreCase in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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:

  1. The method returns true when the input string and the suffix are null. Two null references are considered to be equal.
  2. The method returns false when the input string is null or the suffix is null.

Note: For case-sensitive comparison, refer StringUtils.endsWith

How to import 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;

Syntax


public static boolean endsWithIgnoreCase(final CharSequence str, final CharSequence suffix)

Parameters

The function takes in two parameters:

  • final CharSequence str: Character sequence to check.
  • final CharSequence suffix: the suffix to find in the string

Return value

The function returns true if the string ends with the suffix case insensitive. Otherwise, it returns false.

Code

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));
}
}

Explanation

  1. string = "educative"
    suffix = "TIVE"

The function returns true since the string ends with the suffix in a case-insensitive manner.

  1. string = "educative"
    suffix = "TiVe"

The function returns true since the string ends with the suffix in a case-insensitive manner.

  1. string = "educative"
    suffix = null

The function returns false since the suffix is null.

  1. string = null
    suffix = null

The function returns true since the string and the suffix are null.

Output

The output of the code will be as follows:


true
true
false
true

RELATED TAGS

java
stringutils

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring