Java org.apache.commons.lang3 StringUtils fields, constructors, methods, implement or subclass

Example usage for Java org.apache.commons.lang3 StringUtils fields, constructors, methods, implement or subclass

Introduction

In this page you can find the methods, fields and constructors for org.apache.commons.lang3 StringUtils.

The text is from its open source code.

Subclass

org.apache.commons.lang3.StringUtils has subclasses.
Click this link to see all its subclasses.

Field

StringSPACE
A String for a space character.
StringEMPTY
The empty String "" .
StringLF
A String for linefeed LF ("\n").
StringCR
A String for carriage return CR ("\r").
intINDEX_NOT_FOUND
Represents a failed index search.

Constructor

StringUtils()

StringUtils instances should NOT be constructed in standard programming.

Method

Stringabbreviate(final String str, final int maxWidth)

Abbreviates a String using ellipses.

Stringabbreviate(final String str, int offset, final int maxWidth)

Abbreviates a String using ellipses.

StringabbreviateMiddle(final String str, final String middle, final int length)

Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String.

This abbreviation only occurs if the following criteria is met:

  • Neither the String for abbreviation nor the replacement String are null or empty
  • The length to truncate to is less than the length of the supplied String
  • The length to truncate to is greater than 0
  • The abbreviated String will have enough room for the length supplied replacement String and the first and last characters of the supplied String for abbreviation

Otherwise, the returned String will be the same as the supplied String for abbreviation.

StringappendIfMissing(final String str, final CharSequence suffix, final CharSequence... suffixes)
Appends the suffix to the end of the string if the string does not already end with any the suffixes.
StringappendIfMissingIgnoreCase(final String str, final CharSequence suffix, final CharSequence... suffixes)
Appends the suffix to the end of the string if the string does not already end, case insensitive, with any of the suffixes.
Stringcapitalize(final String str)

Capitalizes a String changing the first letter to title case as per Character#toTitleCase(char) .

Stringcenter(String str, final int size, final char padChar)

Centers a String in a larger String of size size .

Stringcenter(String str, final int size, String padStr)

Centers a String in a larger String of size size .

Stringcenter(final String str, final int size)

Centers a String in a larger String of size size using the space character (' ').

If the size is less than the String length, the String is returned.

Stringchomp(final String str, final String separator)

Removes separator from the end of str if it's there, otherwise leave it alone.

NOTE: This method changed in version 2.0.

Stringchomp(final String str)

Removes one newline from end of a String if it's there, otherwise leave it alone.

Stringchop(final String str)

Remove the last character from a String.

If the String ends in \r\n , then remove both of them.

 StringUtils.chop(null)          = null StringUtils.chop("")            = "" StringUtils.chop("abc \r")      = "abc " StringUtils.chop("abc\n")       = "abc" StringUtils.chop("abc\r\n")     = "abc" StringUtils.chop("abc")         = "ab" StringUtils.chop("abc\nabc")    = "abc\nab" StringUtils.chop("a")           = "" StringUtils.chop("\r")          = "" StringUtils.chop("\n")          = "" StringUtils.chop("\r\n")        = "" 
booleancontains(final CharSequence seq, final int searchChar)

Checks if CharSequence contains a search character, handling null .

booleancontains(final CharSequence seq, final CharSequence searchSeq)

Checks if CharSequence contains a search CharSequence, handling null .

booleancontainsAny(final CharSequence cs, final char... searchChars)

Checks if the CharSequence contains any character in the given set of characters.

A null CharSequence will return false .

booleancontainsAny(final CharSequence cs, final CharSequence searchChars)

Checks if the CharSequence contains any character in the given set of characters.

booleancontainsAny(CharSequence cs, CharSequence... searchCharSequences)

Checks if the CharSequence contains any of the CharSequences in the given array.

A null CharSequence will return false .

booleancontainsIgnoreCase(final CharSequence str, final CharSequence searchStr)

Checks if CharSequence contains a search CharSequence irrespective of case, handling null .

booleancontainsNone(final CharSequence cs, final char... searchChars)

Checks that the CharSequence does not contain certain characters.

A null CharSequence will return true .

booleancontainsNone(final CharSequence cs, final String invalidChars)

Checks that the CharSequence does not contain certain characters.

A null CharSequence will return true .

booleancontainsOnly(final CharSequence cs, final char... valid)

Checks if the CharSequence contains only certain characters.

A null CharSequence will return false .

booleancontainsOnly(final CharSequence cs, final String validChars)

Checks if the CharSequence contains only certain characters.

A null CharSequence will return false .

booleancontainsWhitespace(final CharSequence seq)
Check whether the given CharSequence contains any whitespace characters.
intcountMatches(final CharSequence str, final CharSequence sub)

Counts how many times the substring appears in the larger string.

A null or empty ("") String input returns 0 .

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", null)  = 0 StringUtils.countMatches("abba", "")    = 0 StringUtils.countMatches("abba", "a")   = 2 StringUtils.countMatches("abba", "ab")  = 1 StringUtils.countMatches("abba", "xxx") = 0 
intcountMatches(final CharSequence str, final char ch)

Counts how many times the char appears in the given string.

A null or empty ("") String input returns 0 .

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", 0)  = 0 StringUtils.countMatches("abba", 'a')   = 2 StringUtils.countMatches("abba", 'b')  = 2 StringUtils.countMatches("abba", 'x') = 0 
TdefaultIfBlank(final T str, final T defaultStr)

Returns either the passed in CharSequence, or if the CharSequence is whitespace, empty ("") or null , the value of defaultStr .

 StringUtils.defaultIfBlank(null, "NULL")  = "NULL" StringUtils.defaultIfBlank("", "NULL")    = "NULL" StringUtils.defaultIfBlank(" ", "NULL")   = "NULL" StringUtils.defaultIfBlank("bat", "NULL") = "bat" StringUtils.defaultIfBlank("", null)      = null 
TdefaultIfEmpty(final T str, final T defaultStr)

Returns either the passed in CharSequence, or if the CharSequence is empty or null , the value of defaultStr .

 StringUtils.defaultIfEmpty(null, "NULL")  = "NULL" StringUtils.defaultIfEmpty("", "NULL")    = "NULL" StringUtils.defaultIfEmpty(" ", "NULL")   = " " StringUtils.defaultIfEmpty("bat", "NULL") = "bat" StringUtils.defaultIfEmpty("", null)      = null 
StringdefaultString(final String str)

Returns either the passed in String, or if the String is null , an empty String ("").

 StringUtils.defaultString(null)  = "" StringUtils.defaultString("")    = "" StringUtils.defaultString("bat") = "bat" 
StringdefaultString(final String str, final String defaultStr)

Returns either the passed in String, or if the String is null , the value of defaultStr .

 StringUtils.defaultString(null, "NULL")  = "NULL" StringUtils.defaultString("", "NULL")    = "" StringUtils.defaultString("bat", "NULL") = "bat" 
StringdeleteWhitespace(final String str)

Deletes all whitespaces from a String as defined by Character#isWhitespace(char) .

 StringUtils.deleteWhitespace(null)         = null StringUtils.deleteWhitespace("")           = "" StringUtils.deleteWhitespace("abc")        = "abc" StringUtils.deleteWhitespace("   ab  c  ") = "abc" 
Stringdifference(final String str1, final String str2)

Compares two Strings, and returns the portion where they differ.

booleanendsWith(final CharSequence str, final CharSequence suffix)

Check if a CharSequence ends with a specified suffix.

null s are handled without exceptions.

booleanendsWithAny(final CharSequence string, final CharSequence... searchStrings)

Check if a CharSequence ends with any of an array of specified strings.

 StringUtils.endsWithAny(null, null)      = false StringUtils.endsWithAny(null, new String[] {"abc"})  = false StringUtils.endsWithAny("abcxyz", null)     = false StringUtils.endsWithAny("abcxyz", new String[] {""}) = true StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true 
booleanendsWithIgnoreCase(final CharSequence str, final CharSequence suffix)

Case insensitive check if a CharSequence ends with a specified suffix.

null s are handled without exceptions.

booleanequals(final CharSequence cs1, final CharSequence cs2)

Compares two CharSequences, returning true if they represent equal sequences of characters.

null s are handled without exceptions.

booleanequalsIgnoreCase(final CharSequence str1, final CharSequence str2)

Compares two CharSequences, returning true if they represent equal sequences of characters, ignoring case.

null s are handled without exceptions.

StringgetCommonPrefix(final String... strs)

Compares all Strings in an array and returns the initial sequence of characters that is common to all of them.

For example, getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) -> "i am a "

 StringUtils.getCommonPrefix(null) = "" StringUtils.getCommonPrefix(new String[] {}) = "" StringUtils.getCommonPrefix(new String[] {"abc"}) = "abc" StringUtils.getCommonPrefix(new String[] {null, null}) = "" StringUtils.getCommonPrefix(new String[] {"", ""}) = "" StringUtils.getCommonPrefix(new String[] {"", null}) = "" StringUtils.getCommonPrefix(new String[] {"abc", null, null}) = "" StringUtils.getCommonPrefix(new String[] {null, null, "abc"}) = "" StringUtils.getCommonPrefix(new String[] {"", "abc"}) = "" StringUtils.getCommonPrefix(new String[] {"abc", ""}) = "" StringUtils.getCommonPrefix(new String[] {"abc", "abc"}) = "abc" StringUtils.getCommonPrefix(new String[] {"abc", "a"}) = "a" StringUtils.getCommonPrefix(new String[] {"ab", "abxyz"}) = "ab" StringUtils.getCommonPrefix(new String[] {"abcde", "abxyz"}) = "ab" StringUtils.getCommonPrefix(new String[] {"abcde", "xyz"}) = "" StringUtils.getCommonPrefix(new String[] {"xyz", "abcde"}) = "" StringUtils.getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) = "i am a " 
intgetFuzzyDistance(final CharSequence term, final CharSequence query, final Locale locale)

Find the Fuzzy Distance which indicates the similarity score between two Strings.

This string matching algorithm is similar to the algorithms of editors such as Sublime Text, TextMate, Atom and others.

doublegetJaroWinklerDistance(final CharSequence first, final CharSequence second)

Find the Jaro Winkler Distance which indicates the similarity score between two Strings.

The Jaro measure is the weighted sum of percentage of matched characters from each file and transposed characters.

intgetLevenshteinDistance(CharSequence s, CharSequence t)

Find the Levenshtein distance between two Strings.

This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).

The previous implementation of the Levenshtein distance algorithm was from http://www.merriampark.com/ld.htm

Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError which can occur when my Java implementation is used with very large strings.
This implementation of the Levenshtein distance algorithm is from http://www.merriampark.com/ldjava.htm

 StringUtils.getLevenshteinDistance(null, *)             = IllegalArgumentException StringUtils.getLevenshteinDistance(*, null)             = IllegalArgumentException StringUtils.getLevenshteinDistance("","")               = 0 StringUtils.getLevenshteinDistance("","a")              = 1 StringUtils.getLevenshteinDistance("aaapppp", "")       = 7 StringUtils.getLevenshteinDistance("frog", "fog")       = 1 StringUtils.getLevenshteinDistance("fly", "ant")        = 3 StringUtils.getLevenshteinDistance("elephant", "hippo") = 7 StringUtils.getLevenshteinDistance("hippo", "elephant") = 7 StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") = 8 StringUtils.getLevenshteinDistance("hello", "hallo")    = 1 
intgetLevenshteinDistance(CharSequence s, CharSequence t, final int threshold)

Find the Levenshtein distance between two Strings if it's less than or equal to a given threshold.

This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).

This implementation follows from Algorithms on Strings, Trees and Sequences by Dan Gusfield and Chas Emerick's implementation of the Levenshtein distance algorithm from http://www.merriampark.com/ld.htm

 StringUtils.getLevenshteinDistance(null, *, *)             = IllegalArgumentException StringUtils.getLevenshteinDistance(*, null, *)             = IllegalArgumentException StringUtils.getLevenshteinDistance(*, *, -1)               = IllegalArgumentException StringUtils.getLevenshteinDistance("","", 0)               = 0 StringUtils.getLevenshteinDistance("aaapppp", "", 8)       = 7 StringUtils.getLevenshteinDistance("aaapppp", "", 7)       = 7 StringUtils.getLevenshteinDistance("aaapppp", "", 6))      = -1 StringUtils.getLevenshteinDistance("elephant", "hippo", 7) = 7 StringUtils.getLevenshteinDistance("elephant", "hippo", 6) = -1 StringUtils.getLevenshteinDistance("hippo", "elephant", 7) = 7 StringUtils.getLevenshteinDistance("hippo", "elephant", 6) = -1 
intindexOf(final CharSequence seq, final int searchChar)

Finds the first index within a CharSequence, handling null .

intindexOf(final CharSequence seq, final CharSequence searchSeq)

Finds the first index within a CharSequence, handling null .

intindexOf(final CharSequence seq, final int searchChar, final int startPos)

Finds the first index within a CharSequence from a start position, handling null .

intindexOf(final CharSequence seq, final CharSequence searchSeq, final int startPos)

Finds the first index within a CharSequence, handling null .

intindexOfAny(final CharSequence cs, final char... searchChars)

Search a CharSequence to find the first index of any character in the given set of characters.

A null String will return -1 .

intindexOfAny(final CharSequence cs, final String searchChars)

Search a CharSequence to find the first index of any character in the given set of characters.

A null String will return -1 .

intindexOfAny(final CharSequence str, final CharSequence... searchStrs)

Find the first index of any of a set of potential substrings.

A null CharSequence will return -1 .

intindexOfAnyBut(final CharSequence cs, final char... searchChars)

Searches a CharSequence to find the first index of any character not in the given set of characters.

A null CharSequence will return -1 .

intindexOfAnyBut(final CharSequence seq, final CharSequence searchChars)

Search a CharSequence to find the first index of any character not in the given set of characters.

A null CharSequence will return -1 .

intindexOfDifference(final CharSequence cs1, final CharSequence cs2)

Compares two CharSequences, and returns the index at which the CharSequences begin to differ.

For example, indexOfDifference("i am a machine", "i am a robot") -> 7

 StringUtils.indexOfDifference(null, null) = -1 StringUtils.indexOfDifference("", "") = -1 StringUtils.indexOfDifference("", "abc") = 0 StringUtils.indexOfDifference("abc", "") = 0 StringUtils.indexOfDifference("abc", "abc") = -1 StringUtils.indexOfDifference("ab", "abxyz") = 2 StringUtils.indexOfDifference("abcde", "abxyz") = 2 StringUtils.indexOfDifference("abcde", "xyz") = 0 
intindexOfIgnoreCase(final CharSequence str, final CharSequence searchStr)

Case in-sensitive find of the first index within a CharSequence.

A null CharSequence will return -1 .

intindexOfIgnoreCase(final CharSequence str, final CharSequence searchStr, int startPos)

Case in-sensitive find of the first index within a CharSequence from the specified position.

A null CharSequence will return -1 .

booleanisAllLowerCase(final CharSequence cs)

Checks if the CharSequence contains only lowercase characters.

null will return false .

booleanisAllUpperCase(final CharSequence cs)

Checks if the CharSequence contains only uppercase characters.

null will return false .

booleanisAlpha(final CharSequence cs)

Checks if the CharSequence contains only Unicode letters.

null will return false .

booleanisAlphanumeric(final CharSequence cs)

Checks if the CharSequence contains only Unicode letters or digits.

null will return false .

booleanisAlphanumericSpace(final CharSequence cs)

Checks if the CharSequence contains only Unicode letters, digits or space ( ' ' ).

null will return false .

booleanisAlphaSpace(final CharSequence cs)

Checks if the CharSequence contains only Unicode letters and space (' ').

null will return false An empty CharSequence (length()=0) will return true .

 StringUtils.isAlphaSpace(null)   = false StringUtils.isAlphaSpace("")     = true StringUtils.isAlphaSpace("  ")   = true StringUtils.isAlphaSpace("abc")  = true StringUtils.isAlphaSpace("ab c") = true StringUtils.isAlphaSpace("ab2c") = false StringUtils.isAlphaSpace("ab-c") = false 
booleanisAnyBlank(final CharSequence... css)

Checks if any one of the CharSequences are blank ("") or null and not whitespace only..

 StringUtils.isAnyBlank(null)             = true StringUtils.isAnyBlank(null, "foo")      = true StringUtils.isAnyBlank(null, null)       = true StringUtils.isAnyBlank("", "bar")        = true StringUtils.isAnyBlank("bob", "")        = true StringUtils.isAnyBlank("  bob  ", null)  = true StringUtils.isAnyBlank(" ", "bar")       = true StringUtils.isAnyBlank("foo", "bar")     = false 
booleanisAnyEmpty(final CharSequence... css)

Checks if any one of the CharSequences are empty ("") or null.

 StringUtils.isAnyEmpty(null)             = true StringUtils.isAnyEmpty(null, "foo")      = true StringUtils.isAnyEmpty("", "bar")        = true StringUtils.isAnyEmpty("bob", "")        = true StringUtils.isAnyEmpty("  bob  ", null)  = true StringUtils.isAnyEmpty(" ", "bar")       = false StringUtils.isAnyEmpty("foo", "bar")     = false 
booleanisAsciiPrintable(final CharSequence cs)

Checks if the CharSequence contains only ASCII printable characters.

null will return false .

booleanisBlank(final CharSequence cs)

Checks if a CharSequence is whitespace, empty ("") or null.

 StringUtils.isBlank(null)      = true StringUtils.isBlank("")        = true StringUtils.isBlank(" ")       = true StringUtils.isBlank("bob")     = false StringUtils.isBlank("  bob  ") = false 
booleanisEmpty(final CharSequence cs)

Checks if a CharSequence is empty ("") or null.

 StringUtils.isEmpty(null)      = true StringUtils.isEmpty("")        = true StringUtils.isEmpty(" ")       = false StringUtils.isEmpty("bob")     = false StringUtils.isEmpty("  bob  ") = false 

NOTE: This method changed in Lang version 2.0.

booleanisNoneBlank(final CharSequence... css)

Checks if none of the CharSequences are blank ("") or null and whitespace only..

 StringUtils.isNoneBlank(null)             = false StringUtils.isNoneBlank(null, "foo")      = false StringUtils.isNoneBlank(null, null)       = false StringUtils.isNoneBlank("", "bar")        = false StringUtils.isNoneBlank("bob", "")        = false StringUtils.isNoneBlank("  bob  ", null)  = false StringUtils.isNoneBlank(" ", "bar")       = false StringUtils.isNoneBlank("foo", "bar")     = true 
booleanisNoneEmpty(final CharSequence... css)

Checks if none of the CharSequences are empty ("") or null.

 StringUtils.isNoneEmpty(null)             = false StringUtils.isNoneEmpty(null, "foo")      = false StringUtils.isNoneEmpty("", "bar")        = false StringUtils.isNoneEmpty("bob", "")        = false StringUtils.isNoneEmpty("  bob  ", null)  = false StringUtils.isNoneEmpty(" ", "bar")       = true StringUtils.isNoneEmpty("foo", "bar")     = true 
booleanisNotBlank(final CharSequence cs)

Checks if a CharSequence is not empty (""), not null and not whitespace only.

 StringUtils.isNotBlank(null)      = false StringUtils.isNotBlank("")        = false StringUtils.isNotBlank(" ")       = false StringUtils.isNotBlank("bob")     = true StringUtils.isNotBlank("  bob  ") = true 
booleanisNotEmpty(final CharSequence cs)

Checks if a CharSequence is not empty ("") and not null.

 StringUtils.isNotEmpty(null)      = false StringUtils.isNotEmpty("")        = false StringUtils.isNotEmpty(" ")       = true StringUtils.isNotEmpty("bob")     = true StringUtils.isNotEmpty("  bob  ") = true 
booleanisNumeric(final CharSequence cs)

Checks if the CharSequence contains only Unicode digits.

booleanisNumericSpace(final CharSequence cs)

Checks if the CharSequence contains only Unicode digits or space ( ' ' ).

booleanisWhitespace(final CharSequence cs)

Checks if the CharSequence contains only whitespace.

null will return false .

Stringjoin(final Object[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list.

Stringjoin(final long[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final int[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final short[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final byte[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final char[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final float[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final double[] array, final char separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final Object[] array, final String separator)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list.

Stringjoin(final Iterator iterator, final char separator)

Joins the elements of the provided Iterator into a single String containing the provided elements.

No delimiter is added before or after the list.

Stringjoin(final Iterator iterator, final String separator)

Joins the elements of the provided Iterator into a single String containing the provided elements.

No delimiter is added before or after the list.

Stringjoin(final Iterable iterable, final char separator)

Joins the elements of the provided Iterable into a single String containing the provided elements.

No delimiter is added before or after the list.

Stringjoin(final Iterable iterable, final String separator)

Joins the elements of the provided Iterable into a single String containing the provided elements.

No delimiter is added before or after the list.

Stringjoin(final Object[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list.

Stringjoin(final long[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final int[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final byte[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final short[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final char[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final double[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final float[] array, final char separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

Stringjoin(final Object[] array, String separator, final int startIndex, final int endIndex)

Joins the elements of the provided array into a single String containing the provided list of elements.

No delimiter is added before or after the list.

Stringjoin(final T... elements)

Joins the elements of the provided array into a single String containing the provided list of elements.

No separator is added to the joined String.

intlastIndexOf(final CharSequence seq, final int searchChar)

Finds the last index within a CharSequence, handling null .

intlastIndexOf(final CharSequence seq, final CharSequence searchSeq)

Finds the last index within a CharSequence, handling null .

intlastIndexOf(final CharSequence seq, final int searchChar, final int startPos)

Finds the last index within a CharSequence from a start position, handling null .

intlastIndexOf(final CharSequence seq, final CharSequence searchSeq, final int startPos)

Finds the last index within a CharSequence, handling null .

intlastIndexOfIgnoreCase(final CharSequence str, final CharSequence searchStr)

Case in-sensitive find of the last index within a CharSequence.

A null CharSequence will return -1 .

intlastOrdinalIndexOf(final CharSequence str, final CharSequence searchStr, final int ordinal)

Finds the n-th last index within a String, handling null .

Stringleft(final String str, final int len)

Gets the leftmost len characters of a String.

If len characters are not available, or the String is null , the String will be returned without an exception.

StringleftPad(final String str, final int size, final char padChar)

Left pad a String with a specified character.

Pad to a size of size .

 StringUtils.leftPad(null, *, *)     = null StringUtils.leftPad("", 3, 'z')     = "zzz" StringUtils.leftPad("bat", 3, 'z')  = "bat" StringUtils.leftPad("bat", 5, 'z')  = "zzbat" StringUtils.leftPad("bat", 1, 'z')  = "bat" StringUtils.leftPad("bat", -1, 'z') = "bat" 
StringleftPad(final String str, final int size, String padStr)

Left pad a String with a specified String.

Pad to a size of size .

 StringUtils.leftPad(null, *, *)      = null StringUtils.leftPad("", 3, "z")      = "zzz" StringUtils.leftPad("bat", 3, "yz")  = "bat" StringUtils.leftPad("bat", 5, "yz")  = "yzbat" StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat" StringUtils.leftPad("bat", 1, "yz")  = "bat" StringUtils.leftPad("bat", -1, "yz") = "bat" StringUtils.leftPad("bat", 5, null)  = "  bat" StringUtils.leftPad("bat", 5, "")    = "  bat" 
StringleftPad(final String str, final int size)

Left pad a String with spaces (' ').

The String is padded to the size of size .

 StringUtils.leftPad(null, *)   = null StringUtils.leftPad("", 3)     = "   " StringUtils.leftPad("bat", 3)  = "bat" StringUtils.leftPad("bat", 5)  = "  bat" StringUtils.leftPad("bat", 1)  = "bat" StringUtils.leftPad("bat", -1) = "bat" 
intlength(final CharSequence cs)
Gets a CharSequence length or 0 if the CharSequence is null .
StringlowerCase(final String str)

Converts a String to lower case as per String#toLowerCase() .

A null input String returns null .

 StringUtils.lowerCase(null)  = null StringUtils.lowerCase("")    = "" StringUtils.lowerCase("aBc") = "abc" 

Note: As described in the documentation for String#toLowerCase() , the result of this method is affected by the current locale.

StringlowerCase(final String str, final Locale locale)

Converts a String to lower case as per String#toLowerCase(Locale) .

A null input String returns null .

 StringUtils.lowerCase(null, Locale.ENGLISH)  = null StringUtils.lowerCase("", Locale.ENGLISH)    = "" StringUtils.lowerCase("aBc", Locale.ENGLISH) = "abc" 
Stringmid(final String str, int pos, final int len)

Gets len characters from the middle of a String.

If len characters are not available, the remainder of the String will be returned without an exception.

StringnormalizeSpace(final String str)

Similar to http://www.w3.org/TR/xpath/#function-normalize -space

The function returns the argument string with whitespace normalized by using #trim(String) to remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.

intordinalIndexOf(final CharSequence str, final CharSequence searchStr, final int ordinal)

Finds the n-th index within a CharSequence, handling null .

Stringoverlay(final String str, String overlay, int start, int end)

Overlays part of a String with another String.

A null string input returns null .

StringprependIfMissing(final String str, final CharSequence prefix, final CharSequence... prefixes)
Prepends the prefix to the start of the string if the string does not already start with any of the prefixes.
Stringremove(final String str, final String remove)

Removes all occurrences of a substring from within the source string.

A null source string will return null .

Stringremove(final String str, final char remove)

Removes all occurrences of a character from within the source string.

A null source string will return null .

StringremoveEnd(final String str, final String remove)

Removes a substring only if it is at the end of a source string, otherwise returns the source string.

A null source string will return null .

StringremoveEndIgnoreCase(final String str, final String remove)

Case insensitive removal of a substring if it is at the end of a source string, otherwise returns the source string.

A null source string will return null .

StringremovePattern(final String source, final String regex)
Removes each substring of the source String that matches the given regular expression using the DOTALL option.
StringremoveStart(final String str, final String remove)

Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.

A null source string will return null .

StringremoveStartIgnoreCase(final String str, final String remove)

Case insensitive removal of a substring if it is at the beginning of a source string, otherwise returns the source string.

A null source string will return null .

Stringrepeat(final String str, final int repeat)

Repeat a String repeat times to form a new String.

 StringUtils.repeat(null, 2) = null StringUtils.repeat("", 0)   = "" StringUtils.repeat("", 2)   = "" StringUtils.repeat("a", 3)  = "aaa" StringUtils.repeat("ab", 2) = "abab" StringUtils.repeat("a", -2) = "" 
Stringrepeat(final char ch, final int repeat)

Returns padding using the specified delimiter repeated to a given length.

 StringUtils.repeat('e', 0)  = "" StringUtils.repeat('e', 3)  = "eee" StringUtils.repeat('e', -2) = "" 

Note: this method doesn't not support padding with Unicode Supplementary Characters as they require a pair of char s to be represented.

Stringrepeat(final String str, final String separator, final int repeat)

Repeat a String repeat times to form a new String, with a String separator injected each time.

Stringreplace(final String text, final String searchString, final String replacement)

Replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *)        = null StringUtils.replace("", *, *)          = "" StringUtils.replace("any", null, *)    = "any" StringUtils.replace("any", *, null)    = "any" StringUtils.replace("any", "", *)      = "any" StringUtils.replace("aba", "a", null)  = "aba" StringUtils.replace("aba", "a", "")    = "b" StringUtils.replace("aba", "a", "z")   = "zbz" 
Stringreplace(final String text, final String searchString, final String replacement, int max)

Replaces a String with another String inside a larger String, for the first max values of the search String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *, *)         = null StringUtils.replace("", *, *, *)           = "" StringUtils.replace("any", null, *, *)     = "any" StringUtils.replace("any", *, null, *)     = "any" StringUtils.replace("any", "", *, *)       = "any" StringUtils.replace("any", *, *, 0)        = "any" StringUtils.replace("abaa", "a", null, -1) = "abaa" StringUtils.replace("abaa", "a", "", -1)   = "b" StringUtils.replace("abaa", "a", "z", 0)   = "abaa" StringUtils.replace("abaa", "a", "z", 1)   = "zbaa" StringUtils.replace("abaa", "a", "z", 2)   = "zbza" StringUtils.replace("abaa", "a", "z", -1)  = "zbzz" 
StringreplaceChars(final String str, final char searchChar, final char replaceChar)

Replaces all occurrences of a character in a String with another.

StringreplaceChars(final String str, final String searchChars, String replaceChars)

Replaces multiple characters in a String in one go.

StringreplaceEach(final String text, final String[] searchList, final String[] replacementList)

Replaces all occurrences of Strings within another String.

StringreplaceEachRepeatedly(final String text, final String[] searchList, final String[] replacementList)

Replaces all occurrences of Strings within another String.

StringreplaceOnce(final String text, final String searchString, final String replacement)

Replaces a String with another String inside a larger String, once.

A null reference passed to this method is a no-op.

 StringUtils.replaceOnce(null, *, *)        = null StringUtils.replaceOnce("", *, *)          = "" StringUtils.replaceOnce("any", null, *)    = "any" StringUtils.replaceOnce("any", *, null)    = "any" StringUtils.replaceOnce("any", "", *)      = "any" StringUtils.replaceOnce("aba", "a", null)  = "aba" StringUtils.replaceOnce("aba", "a", "")    = "ba" StringUtils.replaceOnce("aba", "a", "z")   = "zba" 
StringreplacePattern(final String source, final String regex, final String replacement)
Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern#DOTALL option.
Stringreverse(final String str)

Reverses a String as per StringBuilder#reverse() .

A null String returns null .

 StringUtils.reverse(null)  = null StringUtils.reverse("")    = "" StringUtils.reverse("bat") = "tab" 
StringreverseDelimited(final String str, final char separatorChar)

Reverses a String that is delimited by a specific character.

The Strings between the delimiters are not reversed.

Stringright(final String str, final int len)

Gets the rightmost len characters of a String.

If len characters are not available, or the String is null , the String will be returned without an an exception.

StringrightPad(final String str, final int size, final char padChar)

Right pad a String with a specified character.

The String is padded to the size of size .

 StringUtils.rightPad(null, *, *)     = null StringUtils.rightPad("", 3, 'z')     = "zzz" StringUtils.rightPad("bat", 3, 'z')  = "bat" StringUtils.rightPad("bat", 5, 'z')  = "batzz" StringUtils.rightPad("bat", 1, 'z')  = "bat" StringUtils.rightPad("bat", -1, 'z') = "bat" 
StringrightPad(final String str, final int size, String padStr)

Right pad a String with a specified String.

The String is padded to the size of size .

 StringUtils.rightPad(null, *, *)      = null StringUtils.rightPad("", 3, "z")      = "zzz" StringUtils.rightPad("bat", 3, "yz")  = "bat" StringUtils.rightPad("bat", 5, "yz")  = "batyz" StringUtils.rightPad("bat", 8, "yz")  = "batyzyzy" StringUtils.rightPad("bat", 1, "yz")  = "bat" StringUtils.rightPad("bat", -1, "yz") = "bat" StringUtils.rightPad("bat", 5, null)  = "bat  " StringUtils.rightPad("bat", 5, "")    = "bat  " 
StringrightPad(final String str, final int size)

Right pad a String with spaces (' ').

The String is padded to the size of size .

 StringUtils.rightPad(null, *)   = null StringUtils.rightPad("", 3)     = "   " StringUtils.rightPad("bat", 3)  = "bat" StringUtils.rightPad("bat", 5)  = "bat  " StringUtils.rightPad("bat", 1)  = "bat" StringUtils.rightPad("bat", -1) = "bat" 
String[]split(final String str, final char separatorChar)

Splits the provided text into an array, separator specified.

String[]split(final String str, final String separatorChars)

Splits the provided text into an array, separators specified.

String[]split(final String str)

Splits the provided text into an array, using whitespace as the separator.

String[]split(final String str, final String separatorChars, final int max)

Splits the provided text into an array with a maximum length, separators specified.

The separator is not included in the returned String array.

String[]splitByCharacterType(final String str)

Splits a String by Character type as returned by java.lang.Character.getType(char) .

String[]splitByCharacterTypeCamelCase(final String str)

Splits a String by Character type as returned by java.lang.Character.getType(char) .

String[]splitByWholeSeparator(final String str, final String separator)

Splits the provided text into an array, separator string specified.

The separator(s) will not be included in the returned String array.

String[]splitByWholeSeparator(final String str, final String separator, final int max)

Splits the provided text into an array, separator string specified.

String[]splitByWholeSeparatorPreserveAllTokens(final String str, final String separator)

Splits the provided text into an array, separator string specified.

String[]splitByWholeSeparatorPreserveAllTokens(final String str, final String separator, final int max)

Splits the provided text into an array, separator string specified.

String[]splitPreserveAllTokens(final String str, final char separatorChar)

Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators.

String[]splitPreserveAllTokens(final String str, final String separatorChars)

Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators.

String[]splitPreserveAllTokens(final String str, final String separatorChars, final int max)

Splits the provided text into an array with a maximum length, separators specified, preserving all tokens, including empty tokens created by adjacent separators.

The separator is not included in the returned String array.

String[]splitPreserveAllTokens(final String str)

Splits the provided text into an array, using whitespace as the separator, preserving all tokens, including empty tokens created by adjacent separators.

booleanstartsWith(final CharSequence str, final CharSequence prefix)

Check if a CharSequence starts with a specified prefix.

null s are handled without exceptions.

booleanstartsWithAny(final CharSequence string, final CharSequence... searchStrings)

Check if a CharSequence starts with any of an array of specified strings.

 StringUtils.startsWithAny(null, null)      = false StringUtils.startsWithAny(null, new String[] {"abc"})  = false StringUtils.startsWithAny("abcxyz", null)     = false StringUtils.startsWithAny("abcxyz", new String[] {""}) = false StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true 
booleanstartsWithIgnoreCase(final CharSequence str, final CharSequence prefix)

Case insensitive check if a CharSequence starts with a specified prefix.

null s are handled without exceptions.

Stringstrip(final String str)

Strips whitespace from the start and end of a String.

This is similar to #trim(String) but removes whitespace.

Stringstrip(String str, final String stripChars)

Strips any of a set of characters from the start and end of a String.

StringstripAccents(final String input)

Removes diacritics (~= accents) from a string.

String[]stripAll(final String... strs)

Strips whitespace from the start and end of every String in an array.

String[]stripAll(final String[] strs, final String stripChars)

Strips any of a set of characters from the start and end of every String in an array.

Whitespace is defined by Character#isWhitespace(char) .

A new array is returned each time, except for length zero.

StringstripEnd(final String str, final String stripChars)

Strips any of a set of characters from the end of a String.

A null input String returns null .

StringstripStart(final String str, final String stripChars)

Strips any of a set of characters from the start of a String.

A null input String returns null .

StringstripToEmpty(final String str)

Strips whitespace from the start and end of a String returning an empty String if null input.

This is similar to #trimToEmpty(String) but removes whitespace.

StringstripToNull(String str)

Strips whitespace from the start and end of a String returning null if the String is empty ("") after the strip.

This is similar to #trimToNull(String) but removes whitespace.

Stringsubstring(final String str, int start)

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start n characters from the end of the String.

A null String will return null .

Stringsubstring(final String str, int start, int end)

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start/end n characters from the end of the String.

The returned substring starts with the character in the start position and ends before the end position.

StringsubstringAfter(final String str, final String separator)

Gets the substring after the first occurrence of a separator.

StringsubstringAfterLast(final String str, final String separator)

Gets the substring after the last occurrence of a separator.

StringsubstringBefore(final String str, final String separator)

Gets the substring before the first occurrence of a separator.

StringsubstringBeforeLast(final String str, final String separator)

Gets the substring before the last occurrence of a separator.

StringsubstringBetween(final String str, final String open, final String close)

Gets the String that is nested in between two Strings.

StringsubstringBetween(final String str, final String tag)

Gets the String that is nested in between two instances of the same String.

A null input String returns null .

String[]substringsBetween(final String str, final String open, final String close)

Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.

A null input String returns null .

StringswapCase(final String str)

Swaps the case of a String changing upper and title case to lower case, and lower case to upper case.

  • Upper case character converts to Lower case
  • Title case character converts to Lower case
  • Lower case character converts to Upper case

For a word based algorithm, see org.apache.commons.lang3.text.WordUtils#swapCase(String) .

StringtoEncodedString(final byte[] bytes, final Charset charset)
Converts a byte[] to a String using the specified character encoding.
StringtoString(final byte[] bytes, final String charsetName)
Converts a byte[] to a String using the specified character encoding.
Stringtrim(final String str)

Removes control characters (char <= 32) from both ends of this String, handling null by returning null .

The String is trimmed using String#trim() .

StringtrimToEmpty(final String str)

Removes control characters (char <= 32) from both ends of this String returning an empty String ("") if the String is empty ("") after the trim or if it is null .

StringtrimToNull(final String str)

Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null .

Stringuncapitalize(final String str)

Uncapitalizes a String, changing the first letter to lower case as per Character#toLowerCase(char) .

StringupperCase(final String str)

Converts a String to upper case as per String#toUpperCase() .

A null input String returns null .

 StringUtils.upperCase(null)  = null StringUtils.upperCase("")    = "" StringUtils.upperCase("aBc") = "ABC" 

Note: As described in the documentation for String#toUpperCase() , the result of this method is affected by the current locale.

StringupperCase(final String str, final Locale locale)

Converts a String to upper case as per String#toUpperCase(Locale) .

A null input String returns null .

 StringUtils.upperCase(null, Locale.ENGLISH)  = null StringUtils.upperCase("", Locale.ENGLISH)    = "" StringUtils.upperCase("aBc", Locale.ENGLISH) = "ABC" 
Stringwrap(final String str, final char wrapWith)

Wraps a string with a char.

Stringwrap(final String str, final String wrapWith)

Wraps a String with another String.