Trusted answers to developer questions

What is StringUtils.prependIfMissingIgnoreCase in Java?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Overview

prependIfMissingIgnoreCase() is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils that is used to prepend a given prefix at the beginning of the string. This occurs if it does not start with the given list of prefixes without case considerations.


This method is case-insensitive and checks whether the string starts with the given list of prefixes. For the case-sensitive method with the same functionality, refer to What is StringUtils. prependIfMissing in java?

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add 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 prependIfMissingIgnoreCase(final String str, final CharSequence prefix, final CharSequence... prefixes)

Parameters

The function takes on the following parameters, as described below:

  • final String str is the string to check and prepend.
  • final CharSequence prefix is the prefix to prepend to the string.
  • final CharSequence... prefixes is the list of prefixes that are valid.

Return value

The return value is a new string with the prefix prepended. Otherwise, the same string will be returned if the string starts with any of the prefixes.

Code

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

Explanation

  • String = "educaTIVE"
  • Prefix = "edpresso-"
  • Prefixes list = ["hello", "tive"]

The function would prepend the prefix to the string and return edpresso-educaTIVE because the string does not start with any of the values in the prefixes list.

  • String = "EDUCATIVE"
  • Prefix = "edpresso-"
  • Prefixes list = ["hello", "educat", "tive"]

The function would not prepend the prefix to the string, and it returns the same input string EDUCATIVE. This is because the string starts with the "educat" value, which ignores the case considerations in the prefixes list.

Output

The output of the code will be as follows:


edpresso-educaTIVE
EDUCATIVE

RELATED TAGS

java
stringutils

CONTRIBUTOR

abhilash
Attributions:
  1. undefined by undefined
Did you find this helpful?