Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
string
append
communitycreator
ignorecase()

What is StringUtils.appendIfMissingIgnoreCase() 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.

Answers Code

The appendIfMissingIgnoreCase is the static method of the StringUtils that is used to append a given suffix at the end of the string. This occurs if it does not end with the given list of suffixes without case considerations.


This method is case-insensitive and checks whether the string ends with the given list of suffixes. For the case-sensitive method with the same functionality refer to What is StringUtils.appendIfMissing in java?

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>

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 String appendIfMissingIgnoreCase(final String str, final CharSequence suffix, final CharSequence... suffixes)

Parameters

The function takes on the following parameters as described below:

  • final String str is the string to check and append.
  • final CharSequence suffix is the suffix to append to the string.
  • final CharSequence... suffixes is the list of suffixes that are valid terminators.

Return value

The return value is a new string with the suffix appended. Otherwise, the same string will be returned if the string ends with any of the suffixes.

Code

import org.apache.commons.lang3.StringUtils;
public class Main{
public static void main(String[] args){
String beforeAppending = "hello-educaTIVE";
String suffix = "-edpresso";
System.out.println(StringUtils.appendIfMissingIgnoreCase(beforeAppending, suffix, "hello", "educat"));
System.out.println(StringUtils.appendIfMissingIgnoreCase(beforeAppending, suffix, "hello", "educat", "tive"));
}
}

Explanation

  • String = "hello-educatTIVE"
  • Suffix = "-edpresso"
  • Suffixes list = ["hello", "EDUCAT"]

The function would append the suffix to the string and return hello-educatTIVE-edpresso, because the string does not end with any of the values in the suffixes list.

  • String = "hello-educatTIVE"
  • Suffix = "-edpresso"
  • Suffixes list = ["hello", "educat", "tive"]

The function would not append the suffix to the string and returns the same input string hello-educaTIVE, because the string ends with the "tive" value which ignores the case considerations in the suffixes list.

Output

The output of the code will be as follows:


hello-educatTIVE-edpresso
hello-educatTIVE

RELATED TAGS

java
string
append
communitycreator
ignorecase()

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.

Answers Code
Keep Exploring