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;
public static String appendIfMissingIgnoreCase(final String str, final CharSequence suffix, final CharSequence... suffixes)
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.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.
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"));}}
"hello-educatTIVE"
"-edpresso"
["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.
"hello-educatTIVE"
"-edpresso"
["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.
The output of the code will be as follows:
hello-educatTIVE-edpresso
hello-educatTIVE