Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
stringutils

What is StringUtils.prependIfMissing 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.

prependIfMissing is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils class that is used to prepend a given prefix at the beginning of a string if it does not already end with one on the given list.


This method is case-sensitive, which means it checks whether the string starts with the given list of prefixes.


For a case-insensitive method with the same functionality, refer to this shot.

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 prependIfMissing(final String str, final CharSequence prefix, final CharSequence... prefixes)

Parameters

The function takes the following parameters:

  • 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 in case 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 = "hello-";
System.out.println(StringUtils.prependIfMissing(beforePrepending, prefix, "hello", "tive"));
System.out.println(StringUtils.prependIfMissing(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 hello-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 prefix the prefix to the string and returns the same input string educative, because the string starts with the "educat" value in the prefixes list.

Output

The output of the code will be as follows:

hello-educative
educative

RELATED TAGS

java
communitycreator
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