org.apache.commons.lang
Class StringUtils

java.lang.Object
  extended byorg.apache.commons.lang.StringUtils

public class StringUtils
extends java.lang.Object

Operations on String that are null safe.

The StringUtils class defines certain words related to String handling.

StringUtils handles null input Strings quietly. That is to say that a null input will return null. Where a boolean or int is being returned details vary by method.

A side effect of the null handling is that a NullPointerException should be considered a bug in StringUtils (except for deprecated methods).

Methods in this class give sample code to explain their operation. The symbol * is used to indicate any input including null.

Since:
1.0
Version:
$Id: StringUtils.java 635447 2008-03-10 06:27:09Z bayard $
Author:
Apache Jakarta Turbine, Jon S. Stevens, Daniel L. Rall, Greg Coladonato, Ed Korthof, Rand McNeely, Stephen Colebourne, Fredrik Westermarck, Holger Krauth, Alexander Day Chaffee, Henning P. Schmiedehausen, Arun Mammen Thomas, Gary Gregory, Phil Steitz, Al Chou, Michael Davey, Reuben Sivan, Chris Hyzer, Scott Johnson
See Also:
String

Field Summary
static java.lang.String EMPTY
          The empty String "".
static int INDEX_NOT_FOUND
          Represents a failed index search.
 
Constructor Summary
StringUtils()
          StringUtils instances should NOT be constructed in standard programming.
 
Method Summary
static java.lang.String abbreviate(java.lang.String str, int maxWidth)
          Abbreviates a String using ellipses.
static java.lang.String abbreviate(java.lang.String str, int offset, int maxWidth)
          Abbreviates a String using ellipses.
static java.lang.String capitalise(java.lang.String str)
          Deprecated. Use the standardly named capitalize(String). Method will be removed in Commons Lang 3.0.
static java.lang.String capitaliseAllWords(java.lang.String str)
          Deprecated. Use the relocated WordUtils.capitalize(String). Method will be removed in Commons Lang 3.0.
static java.lang.String capitalize(java.lang.String str)
          Capitalizes a String changing the first letter to title case as per Character.toTitleCase(char).
static java.lang.String center(java.lang.String str, int size)
          Centers a String in a larger String of size size using the space character (' ').
static java.lang.String center(java.lang.String str, int size, char padChar)
          Centers a String in a larger String of size size.
static java.lang.String center(java.lang.String str, int size, java.lang.String padStr)
          Centers a String in a larger String of size size.
static java.lang.String chomp(java.lang.String str)
          Removes one newline from end of a String if it's there, otherwise leave it alone.
static java.lang.String chomp(java.lang.String str, java.lang.String separator)
          Removes separator from the end of str if it's there, otherwise leave it alone.
static java.lang.String chompLast(java.lang.String str)
          Deprecated. Use chomp(String) instead. Method will be removed in Commons Lang 3.0.
static java.lang.String chompLast(java.lang.String str, java.lang.String sep)
          Deprecated. Use chomp(String,String) instead. Method will be removed in Commons Lang 3.0.
static java.lang.String chop(java.lang.String str)
          Remove the last character from a String.
static java.lang.String chopNewline(java.lang.String str)
          Deprecated. Use chomp(String) instead. Method will be removed in Commons Lang 3.0.
static java.lang.String clean(java.lang.String str)
          Deprecated. Use the clearer named trimToEmpty(String). Method will be removed in Commons Lang 3.0.
static java.lang.String concatenate(java.lang.Object[] array)
          Deprecated. Use the better named join(Object[]) instead. Method will be removed in Commons Lang 3.0.
static boolean contains(java.lang.String str, char searchChar)
          Checks if String contains a search character, handling null.
static boolean contains(java.lang.String str, java.lang.String searchStr)
          Checks if String contains a search String, handling null.
static boolean containsAny(java.lang.String str, char[] searchChars)
          Checks if the String contains any character in the given set of characters.
static boolean containsAny(java.lang.String str, java.lang.String searchChars)
           Checks if the String contains any character in the given set of characters.
static boolean containsIgnoreCase(java.lang.String str, java.lang.String searchStr)
          Checks if String contains a search String irrespective of case, handling null.
static boolean containsNone(java.lang.String str, char[] invalidChars)
          Checks that the String does not contain certain characters.
static boolean containsNone(java.lang.String str, java.lang.String invalidChars)
          Checks that the String does not contain certain characters.
static boolean containsOnly(java.lang.String str, char[] valid)
          Checks if the String contains only certain characters.
static boolean containsOnly(java.lang.String str, java.lang.String validChars)
          Checks if the String contains only certain characters.
static int countMatches(java.lang.String str, java.lang.String sub)
          Counts how many times the substring appears in the larger String.
static java.lang.String defaultIfEmpty(java.lang.String str, java.lang.String defaultStr)
          Returns either the passed in String, or if the String is empty or null, the value of defaultStr.
static java.lang.String defaultString(java.lang.String str)
          Returns either the passed in String, or if the String is null, an empty String ("").
static java.lang.String defaultString(java.lang.String str, java.lang.String defaultStr)
          Returns either the passed in String, or if the String is null, the value of defaultStr.
static java.lang.String deleteSpaces(java.lang.String str)
          Deprecated. Use the better localized deleteWhitespace(String). Method will be removed in Commons Lang 3.0.
static java.lang.String deleteWhitespace(java.lang.String str)
          Deletes all whitespaces from a String as defined by Character.isWhitespace(char).
static java.lang.String difference(java.lang.String str1, java.lang.String str2)
          Compares two Strings, and returns the portion where they differ.
static boolean endsWith(java.lang.String str, java.lang.String suffix)
          Check if a String ends with a specified suffix.
static boolean endsWithIgnoreCase(java.lang.String str, java.lang.String suffix)
          Case insensitive check if a String ends with a specified suffix.
static boolean equals(java.lang.String str1, java.lang.String str2)
          Compares two Strings, returning true if they are equal.
static boolean equalsIgnoreCase(java.lang.String str1, java.lang.String str2)
          Compares two Strings, returning true if they are equal ignoring the case.
static java.lang.String escape(java.lang.String str)
          Deprecated. Use StringEscapeUtils.escapeJava(String) This method will be removed in Commons Lang 3.0
static java.lang.String getChomp(java.lang.String str, java.lang.String sep)
          Deprecated. Use substringAfterLast(String, String) instead (although this doesn't include the separator) Method will be removed in Commons Lang 3.0.
static java.lang.String getCommonPrefix(java.lang.String[] strs)
          Compares all Strings in an array and returns the initial sequence of characters that is common to all of them.
static int getLevenshteinDistance(java.lang.String s, java.lang.String t)
          Find the Levenshtein distance between two Strings.
static java.lang.String getNestedString(java.lang.String str, java.lang.String tag)
          Deprecated. Use the better named substringBetween(String, String). Method will be removed in Commons Lang 3.0.
static java.lang.String getNestedString(java.lang.String str, java.lang.String open, java.lang.String close)
          Deprecated. Use the better named substringBetween(String, String, String). Method will be removed in Commons Lang 3.0.
static java.lang.String getPrechomp(java.lang.String str, java.lang.String sep)
          Deprecated. Use substringBefore(String,String) instead (although this doesn't include the separator). Method will be removed in Commons Lang 3.0.
static int indexOf(java.lang.String str, char searchChar)
          Finds the first index within a String, handling null.
static int indexOf(java.lang.String str, char searchChar, int startPos)
          Finds the first index within a String from a start position, handling null.
static int indexOf(java.lang.String str, java.lang.String searchStr)
          Finds the first index within a String, handling null.
static int indexOf(java.lang.String str, java.lang.String searchStr, int startPos)
          Finds the first index within a String, handling null.
static int indexOfAny(java.lang.String str, char[] searchChars)
          Search a String to find the first index of any character in the given set of characters.
static int indexOfAny(java.lang.String str, java.lang.String searchChars)
          Search a String to find the first index of any character in the given set of characters.
static int indexOfAny(java.lang.String str, java.lang.String[] searchStrs)
          Find the first index of any of a set of potential substrings.
static int indexOfAnyBut(java.lang.String str, char[] searchChars)
          Search a String to find the first index of any character not in the given set of characters.
static int indexOfAnyBut(java.lang.String str, java.lang.String searchChars)
          Search a String to find the first index of any character not in the given set of characters.
static int indexOfDifference(java.lang.String[] strs)
          Compares all Strings in an array and returns the index at which the Strings begin to differ.
static int indexOfDifference(java.lang.String str1, java.lang.String str2)
          Compares two Strings, and returns the index at which the Strings begin to differ.
static boolean isAlpha(java.lang.String str)
          Checks if the String contains only unicode letters.
static boolean isAlphanumeric(java.lang.String str)
          Checks if the String contains only unicode letters or digits.
static boolean isAlphanumericSpace(java.lang.String str)
          Checks if the String contains only unicode letters, digits or space (' ').
static boolean isAlphaSpace(java.lang.String str)
          Checks if the String contains only unicode letters and space (' ').
static boolean isAsciiPrintable(java.lang.String str)
          Checks if the string contains only ASCII printable characters.
static boolean isBlank(java.lang.String str)
          Checks if a String is whitespace, empty ("") or null.
static boolean isEmpty(java.lang.String str)
          Checks if a String is empty ("") or null.
static boolean isNotBlank(java.lang.String str)
          Checks if a String is not empty (""), not null and not whitespace only.
static boolean isNotEmpty(java.lang.String str)
          Checks if a String is not empty ("") and not null.
static boolean isNumeric(java.lang.String str)
          Checks if the String contains only unicode digits.
static boolean isNumericSpace(java.lang.String str)
          Checks if the String contains only unicode digits or space (' ').
static boolean isWhitespace(java.lang.String str)
          Checks if the String contains only whitespace.
static java.lang.String join(java.util.Collection collection, char separator)
          Joins the elements of the provided Collection into a single String containing the provided elements.
static java.lang.String join(java.util.Collection collection, java.lang.String separator)
          Joins the elements of the provided Collection into a single String containing the provided elements.
static java.lang.String join(java.util.Iterator iterator, char separator)
          Joins the elements of the provided Iterator into a single String containing the provided elements.
static java.lang.String join(java.util.Iterator iterator, java.lang.String separator)
          Joins the elements of the provided Iterator into a single String containing the provided elements.
static java.lang.String join(java.lang.Object[] array)
          Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(java.lang.Object[] array, char separator)
          Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(java.lang.Object[] array, char separator, int startIndex, int endIndex)
          Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(java.lang.Object[] array, java.lang.String separator)
          Joins the elements of the provided array into a single String containing the provided list of elements.
static java.lang.String join(java.lang.Object[] array, java.lang.String separator, int startIndex, int endIndex)
          Joins the elements of the provided array into a single String containing the provided list of elements.
static int lastIndexOf(java.lang.String str, char searchChar)
          Finds the last index within a String, handling null.
static int lastIndexOf(java.lang.String str, char searchChar, int startPos)
          Finds the last index within a String from a start position, handling null.
static int lastIndexOf(java.lang.String str, java.lang.String searchStr)
          Finds the last index within a String, handling null.
static int lastIndexOf(java.lang.String str, java.lang.String searchStr, int startPos)
          Finds the first index within a String, handling null.
static int lastIndexOfAny(java.lang.String str, java.lang.String[] searchStrs)
          Find the latest index of any of a set of potential substrings.
static java.lang.String left(java.lang.String str, int len)
          Gets the leftmost len characters of a String.
static java.lang.String leftPad(java.lang.String str, int size)
          Left pad a String with spaces (' ').
static java.lang.String leftPad(java.lang.String str, int size, char padChar)
          Left pad a String with a specified character.
static java.lang.String leftPad(java.lang.String str, int size, java.lang.String padStr)
          Left pad a String with a specified String.
static int length(java.lang.String str)
          Gets a String's length or 0 if the String is null.
static java.lang.String lowerCase(java.lang.String str)
          Converts a String to lower case as per String.toLowerCase().
static java.lang.String mid(java.lang.String str, int pos, int len)
          Gets len characters from the middle of a String.
static int ordinalIndexOf(java.lang.String str, java.lang.String searchStr, int ordinal)
          Finds the n-th index within a String, handling null.
static java.lang.String overlay(java.lang.String str, java.lang.String overlay, int start, int end)
          Overlays part of a String with another String.
static java.lang.String overlayString(java.lang.String text, java.lang.String overlay, int start, int end)
          Deprecated. Use better named overlay(String, String, int, int) instead. Method will be removed in Commons Lang 3.0.
static java.lang.String prechomp(java.lang.String str, java.lang.String sep)
          Deprecated. Use substringAfter(String,String) instead. Method will be removed in Commons Lang 3.0.
static java.lang.String remove(java.lang.String str, char remove)
          Removes all occurrences of a character from within the source string.
static java.lang.String remove(java.lang.String str, java.lang.String remove)
          Removes all occurrences of a substring from within the source string.
static java.lang.String removeEnd(java.lang.String str, java.lang.String remove)
          Removes a substring only if it is at the end of a source string, otherwise returns the source string.
static java.lang.String removeEndIgnoreCase(java.lang.String str, java.lang.String remove)
          Case insensitive removal of a substring if it is at the end of a source string, otherwise returns the source string.
static java.lang.String removeStart(java.lang.String str, java.lang.String remove)
          Removes a substring only if it is at the begining of a source string, otherwise returns the source string.
static java.lang.String removeStartIgnoreCase(java.lang.String str, java.lang.String remove)
          Case insensitive removal of a substring if it is at the begining of a source string, otherwise returns the source string.
static java.lang.String repeat(java.lang.String str, int repeat)
          Repeat a String repeat times to form a new String.
static java.lang.String replace(java.lang.String text, java.lang.String searchString, java.lang.String replacement)
          Replaces all occurrences of a String within another String.
static java.lang.String replace(java.lang.String text, java.lang.String searchString, java.lang.String replacement, int max)
          Replaces a String with another String inside a larger String, for the first max values of the search String.
static java.lang.String replaceChars(java.lang.String str, char searchChar, char replaceChar)
          Replaces all occurrences of a character in a String with another.
static java.lang.String replaceChars(java.lang.String str, java.lang.String searchChars, java.lang.String replaceChars)
          Replaces multiple characters in a String in one go.
static java.lang.String replaceEach(java.lang.String text, java.lang.String[] searchList, java.lang.String[] replacementList)
           Replaces all occurrences of Strings within another String.
static java.lang.String replaceEachRepeatedly(java.lang.String text, java.lang.String[] searchList, java.lang.String[] replacementList)
           Replaces all occurrences of Strings within another String.
static java.lang.String replaceOnce(java.lang.String text, java.lang.String searchString, java.lang.String replacement)
          Replaces a String with another String inside a larger String, once.
static java.lang.String reverse(java.lang.String str)
          Reverses a String as per StringBuffer.reverse().
static java.lang.String reverseDelimited(java.lang.String str, char separatorChar)
          Reverses a String that is delimited by a specific character.
static java.lang.String reverseDelimitedString(java.lang.String str, java.lang.String separatorChars)
          Deprecated. Use reverseDelimited(String, char) instead. This method is broken as the join doesn't know which char to use. Method will be removed in Commons Lang 3.0.
static java.lang.String right(java.lang.String str, int len)
          Gets the rightmost len characters of a String.
static java.lang.String rightPad(java.lang.String str, int size)
          Right pad a String with spaces (' ').
static java.lang.String rightPad(java.lang.String str, int size, char padChar)
          Right pad a String with a specified character.
static java.lang.String rightPad(java.lang.String str, int size, java.lang.String padStr)
          Right pad a String with a specified String.
static java.lang.String[] split(java.lang.String str)
          Splits the provided text into an array, using whitespace as the separator.
static java.lang.String[] split(java.lang.String str, char separatorChar)
          Splits the provided text into an array, separator specified.
static java.lang.String[] split(java.lang.String str, java.lang.String separatorChars)
          Splits the provided text into an array, separators specified.
static java.lang.String[] split(java.lang.String str, java.lang.String separatorChars, int max)
          Splits the provided text into an array with a maximum length, separators specified.
static java.lang.String[] splitByCharacterType(java.lang.String str)
          Splits a String by Character type as returned by java.lang.Character.getType(char).
static java.lang.String[] splitByCharacterTypeCamelCase(java.lang.String str)
          Splits a String by Character type as returned by java.lang.Character.getType(char).
static java.lang.String[] splitByWholeSeparator(java.lang.String str, java.lang.String separator)
          Splits the provided text into an array, separator string specified.
static java.lang.String[] splitByWholeSeparator(java.lang.String str, java.lang.String separator, int max)
          Splits the provided text into an array, separator string specified.
static java.lang.String[] splitByWholeSeparatorPreserveAllTokens(java.lang.String str, java.lang.String separator)
          Splits the provided text into an array, separator string specified.
static java.lang.String[] splitByWholeSeparatorPreserveAllTokens(java.lang.String str, java.lang.String separator, int max)
          Splits the provided text into an array, separator string specified.
static java.lang.String[] splitPreserveAllTokens(java.lang.String str)
          Splits the provided text into an array, using whitespace as the separator, preserving all tokens, including empty tokens created by adjacent separators.
static java.lang.String[] splitPreserveAllTokens(java.lang.String str, char separatorChar)
          Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators.
static java.lang.String[] splitPreserveAllTokens(java.lang.String str, java.lang.String separatorChars)
          Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators.
static java.lang.String[] splitPreserveAllTokens(java.lang.String str, java.lang.String separatorChars, 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.
static boolean startsWith(java.lang.String str, java.lang.String prefix)
          Check if a String starts with a specified prefix.
static boolean startsWithIgnoreCase(java.lang.String str, java.lang.String prefix)
          Case insensitive check if a String starts with a specified prefix.
static java.lang.String strip(java.lang.String str)
          Strips whitespace from the start and end of a String.
static java.lang.String strip(java.lang.String str, java.lang.String stripChars)
          Strips any of a set of characters from the start and end of a String.
static java.lang.String[] stripAll(java.lang.String[] strs)
          Strips whitespace from the start and end of every String in an array.
static java.lang.String[] stripAll(java.lang.String[] strs, java.lang.String stripChars)
          Strips any of a set of characters from the start and end of every String in an array.
static java.lang.String stripEnd(java.lang.String str, java.lang.String stripChars)
          Strips any of a set of characters from the end of a String.
static java.lang.String stripStart(java.lang.String str, java.lang.String stripChars)
          Strips any of a set of characters from the start of a String.
static java.lang.String stripToEmpty(java.lang.String str)
          Strips whitespace from the start and end of a String returning an empty String if null input.
static java.lang.String stripToNull(java.lang.String str)
          Strips whitespace from the start and end of a String returning null if the String is empty ("") after the strip.
static java.lang.String substring(java.lang.String str, int start)
          Gets a substring from the specified String avoiding exceptions.
static java.lang.String substring(java.lang.String str, int start, int end)
          Gets a substring from the specified String avoiding exceptions.
static java.lang.String substringAfter(java.lang.String str, java.lang.String separator)
          Gets the substring after the first occurrence of a separator.
static java.lang.String substringAfterLast(java.lang.String str, java.lang.String separator)
          Gets the substring after the last occurrence of a separator.
static java.lang.String substringBefore(java.lang.String str, java.lang.String separator)
          Gets the substring before the first occurrence of a separator.
static java.lang.String substringBeforeLast(java.lang.String str, java.lang.String separator)
          Gets the substring before the last occurrence of a separator.
static java.lang.String substringBetween(java.lang.String str, java.lang.String tag)
          Gets the String that is nested in between two instances of the same String.
static java.lang.String substringBetween(java.lang.String str, java.lang.String open, java.lang.String close)
          Gets the String that is nested in between two Strings.
static java.lang.String[] substringsBetween(java.lang.String str, java.lang.String open, java.lang.String close)
          Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.
static java.lang.String swapCase(java.lang.String str)
          Swaps the case of a String changing upper and title case to lower case, and lower case to upper case.
static java.lang.String trim(java.lang.String str)
          Removes control characters (char <= 32) from both ends of this String, handling null by returning null.
static java.lang.String trimToEmpty(java.lang.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.
static java.lang.String trimToNull(java.lang.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.
static java.lang.String uncapitalise(java.lang.String str)
          Deprecated. Use the standardly named uncapitalize(String). Method will be removed in Commons Lang 3.0.
static java.lang.String uncapitalize(java.lang.String str)
          Uncapitalizes a String changing the first letter to title case as per Character.toLowerCase(char).
static java.lang.String upperCase(java.lang.String str)
          Converts a String to upper case as per String.toUpperCase().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY

public static final java.lang.String EMPTY
The empty String "".

Since:
2.0
See Also:
Constant Field Values

INDEX_NOT_FOUND

public static final int INDEX_NOT_FOUND
Represents a failed index search.

Since:
2.1
See Also:
Constant Field Values
Constructor Detail

StringUtils

public StringUtils()

StringUtils instances should NOT be constructed in standard programming. Instead, the class should be used as StringUtils.trim(" foo ");.

This constructor is public to permit tools that require a JavaBean instance to operate.

Method Detail

isEmpty

public static boolean isEmpty(java.lang.String str)

Checks if a String 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. It no longer trims the String. That functionality is available in isBlank().

Parameters:
str - the String to check, may be null
Returns:
true if the String is empty or null

isNotEmpty

public static boolean isNotEmpty(java.lang.String str)

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

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

Parameters:
str - the String to check, may be null
Returns:
true if the String is not empty and not null

isBlank

public static boolean isBlank(java.lang.String str)

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

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

Parameters:
str - the String to check, may be null
Returns:
true if the String is null, empty or whitespace
Since:
2.0

isNotBlank

public static boolean isNotBlank(java.lang.String str)

Checks if a String 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
 

Parameters:
str - the String to check, may be null
Returns:
true if the String is not empty and not null and not whitespace
Since:
2.0

clean

public static java.lang.String clean(java.lang.String str)
Deprecated. Use the clearer named trimToEmpty(String). Method will be removed in Commons Lang 3.0.

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

 StringUtils.clean(null)          = ""
 StringUtils.clean("")            = ""
 StringUtils.clean("abc")         = "abc"
 StringUtils.clean("    abc    ") = "abc"
 StringUtils.clean("     ")       = ""
 

Parameters:
str - the String to clean, may be null
Returns:
the trimmed text, never null
See Also:
String.trim()

trim

public static java.lang.String trim(java.lang.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(). Trim removes start and end characters <= 32. To strip whitespace use strip(String).

To trim your choice of characters, use the strip(String, String) methods.

 StringUtils.trim(null)          = null
 StringUtils.trim("")            = ""
 StringUtils.trim("     ")       = ""
 StringUtils.trim("abc")         = "abc"
 StringUtils.trim("    abc    ") = "abc"
 

Parameters:
str - the String to be trimmed, may be null
Returns:
the trimmed string, null if null String input

trimToNull

public static java.lang.String trimToNull(java.lang.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.

The String is trimmed using String.trim(). Trim removes start and end characters <= 32. To strip whitespace use stripToNull(String).

 StringUtils.trimToNull(null)          = null
 StringUtils.trimToNull("")            = null
 StringUtils.trimToNull("     ")       = null
 StringUtils.trimToNull("abc")         = "abc"
 StringUtils.trimToNull("    abc    ") = "abc"
 

Parameters:
str - the String to be trimmed, may be null
Returns:
the trimmed String, null if only chars <= 32, empty or null String input
Since:
2.0

trimToEmpty

public static java.lang.String trimToEmpty(java.lang.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.

The String is trimmed using String.trim(). Trim removes start and end characters <= 32. To strip whitespace use stripToEmpty(String).

 StringUtils.trimToEmpty(null)          = ""
 StringUtils.trimToEmpty("")            = ""
 StringUtils.trimToEmpty("     ")       = ""
 StringUtils.trimToEmpty("abc")         = "abc"
 StringUtils.trimToEmpty("    abc    ") = "abc"
 

Parameters:
str - the String to be trimmed, may be null
Returns:
the trimmed String, or an empty String if null input
Since:
2.0

strip

public static java.lang.String strip(java.lang.String str)

Strips whitespace from the start and end of a String.

This is similar to trim(String) but removes whitespace. Whitespace is defined by Character.isWhitespace(char).

A null input String returns null.

 StringUtils.strip(null)     = null
 StringUtils.strip("")       = ""
 StringUtils.strip("   ")    = ""
 StringUtils.strip("abc")    = "abc"
 StringUtils.strip("  abc")  = "abc"
 StringUtils.strip("abc  ")  = "abc"
 StringUtils.strip(" abc ")  = "abc"
 StringUtils.strip(" ab c ") = "ab c"
 

Parameters:
str - the String to remove whitespace from, may be null
Returns:
the stripped String, null if null String input

stripToNull

public static java.lang.String stripToNull(java.lang.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. Whitespace is defined by Character.isWhitespace(char).

 StringUtils.stripToNull(null)     = null
 StringUtils.stripToNull("")       = null
 StringUtils.stripToNull("   ")    = null
 StringUtils.stripToNull("abc")    = "abc"
 StringUtils.stripToNull("  abc")  = "abc"
 StringUtils.stripToNull("abc  ")  = "abc"
 StringUtils.stripToNull(" abc ")  = "abc"
 StringUtils.stripToNull(" ab c ") = "ab c"
 

Parameters:
str - the String to be stripped, may be null
Returns:
the stripped String, null if whitespace, empty or null String input
Since:
2.0

stripToEmpty

public static java.lang.String stripToEmpty(java.lang.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. Whitespace is defined by Character.isWhitespace(char).

 StringUtils.stripToEmpty(null)     = ""
 StringUtils.stripToEmpty("")       = ""
 StringUtils.stripToEmpty("   ")    = ""
 StringUtils.stripToEmpty("abc")    = "abc"
 StringUtils.stripToEmpty("  abc")  = "abc"
 StringUtils.stripToEmpty("abc  ")  = "abc"
 StringUtils.stripToEmpty(" abc ")  = "abc"
 StringUtils.stripToEmpty(" ab c ") = "ab c"
 

Parameters:
str - the String to be stripped, may be null
Returns:
the trimmed String, or an empty String if null input
Since:
2.0

strip

public static java.lang.String strip(java.lang.String str,
                                     java.lang.String stripChars)

Strips any of a set of characters from the start and end of a String. This is similar to String.trim() but allows the characters to be stripped to be controlled.

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char). Alternatively use strip(String).

 StringUtils.strip(null, *)          = null
 StringUtils.strip("", *)            = ""
 StringUtils.strip("abc", null)      = "abc"
 StringUtils.strip("  abc", null)    = "abc"
 StringUtils.strip("abc  ", null)    = "abc"
 StringUtils.strip(" abc ", null)    = "abc"
 StringUtils.strip("  abcyx", "xyz") = "  abc"
 

Parameters:
str - the String to remove characters from, may be null
stripChars - the characters to remove, null treated as whitespace
Returns:
the stripped String, null if null String input

stripStart

public static java.lang.String stripStart(java.lang.String str,
                                          java.lang.String stripChars)

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

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

 StringUtils.stripStart(null, *)          = null
 StringUtils.stripStart("", *)            = ""
 StringUtils.stripStart("abc", "")        = "abc"
 StringUtils.stripStart("abc", null)      = "abc"
 StringUtils.stripStart("  abc", null)    = "abc"
 StringUtils.stripStart("abc  ", null)    = "abc  "
 StringUtils.stripStart(" abc ", null)    = "abc "
 StringUtils.stripStart("yxabc  ", "xyz") = "abc  "
 

Parameters:
str - the String to remove characters from, may be null
stripChars - the characters to remove, null treated as whitespace
Returns:
the stripped String, null if null String input

stripEnd

public static java.lang.String stripEnd(java.lang.String str,
                                        java.lang.String stripChars)

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

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

 StringUtils.stripEnd(null, *)          = null
 StringUtils.stripEnd("", *)            = ""
 StringUtils.stripEnd("abc", "")        = "abc"
 StringUtils.stripEnd("abc", null)      = "abc"
 StringUtils.stripEnd("  abc", null)    = "  abc"
 StringUtils.stripEnd("abc  ", null)    = "abc"
 StringUtils.stripEnd(" abc ", null)    = " abc"
 StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
 

Parameters:
str - the String to remove characters from, may be null
stripChars - the characters to remove, null treated as whitespace
Returns:
the stripped String, null if null String input

stripAll

public static java.lang.String[] stripAll(java.lang.String[] strs)

Strips whitespace 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. A null array will return null. An empty array will return itself. A null array entry will be ignored.

 StringUtils.stripAll(null)             = null
 StringUtils.stripAll([])               = []
 StringUtils.stripAll(["abc", "  abc"]) = ["abc", "abc"]
 StringUtils.stripAll(["abc  ", null])  = ["abc", null]
 

Parameters:
strs - the array to remove whitespace from, may be null
Returns:
the stripped Strings, null if null array input

stripAll

public static java.lang.String[] stripAll(java.lang.String[] strs,
                                          java.lang.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. A null array will return null. An empty array will return itself. A null array entry will be ignored. A null stripChars will strip whitespace as defined by Character.isWhitespace(char).

 StringUtils.stripAll(null, *)                = null
 StringUtils.stripAll([], *)                  = []
 StringUtils.stripAll(["abc", "  abc"], null) = ["abc", "abc"]
 StringUtils.stripAll(["abc  ", null], null)  = ["abc", null]
 StringUtils.stripAll(["abc  ", null], "yz")  = ["abc  ", null]
 StringUtils.stripAll(["yabcz", null], "yz")  = ["abc", null]
 

Parameters:
strs - the array to remove characters from, may be null
stripChars - the characters to remove, null treated as whitespace
Returns:
the stripped Strings, null if null array input

equals

public static boolean equals(java.lang.String str1,
                             java.lang.String str2)

Compares two Strings, returning true if they are equal.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

 StringUtils.equals(null, null)   = true
 StringUtils.equals(null, "abc")  = false
 StringUtils.equals("abc", null)  = false
 StringUtils.equals("abc", "abc") = true
 StringUtils.equals("abc", "ABC") = false
 

Parameters:
str1 - the first String, may be null
str2 - the second String, may be null
Returns:
true if the Strings are equal, case sensitive, or both null
See Also:
String.equals(Object)

equalsIgnoreCase

public static boolean equalsIgnoreCase(java.lang.String str1,
                                       java.lang.String str2)

Compares two Strings, returning true if they are equal ignoring the case.

nulls are handled without exceptions. Two null references are considered equal. Comparison is case insensitive.

 StringUtils.equalsIgnoreCase(null, null)   = true
 StringUtils.equalsIgnoreCase(null, "abc")  = false
 StringUtils.equalsIgnoreCase("abc", null)  = false
 StringUtils.equalsIgnoreCase("abc", "abc") = true
 StringUtils.equalsIgnoreCase("abc", "ABC") = true
 

Parameters:
str1 - the first String, may be null
str2 - the second String, may be null
Returns:
true if the Strings are equal, case insensitive, or both null
See Also:
String.equalsIgnoreCase(String)

indexOf

public static int indexOf(java.lang.String str,
                          char searchChar)

Finds the first index within a String, handling null. This method uses String.indexOf(int).

A null or empty ("") String will return -1.

 StringUtils.indexOf(null, *)         = -1
 StringUtils.indexOf("", *)           = -1
 StringUtils.indexOf("aabaabaa", 'a') = 0
 StringUtils.indexOf("aabaabaa", 'b') = 2
 

Parameters:
str - the String to check, may be null
searchChar - the character to find
Returns:
the first index of the search character, -1 if no match or null string input
Since:
2.0

indexOf

public static int indexOf(java.lang.String str,
                          char searchChar,
                          int startPos)

Finds the first index within a String from a start position, handling null. This method uses String.indexOf(int, int).

A null or empty ("") String will return -1. A negative start position is treated as zero. A start position greater than the string length returns -1.

 StringUtils.indexOf(null, *, *)          = -1
 StringUtils.indexOf("", *, *)            = -1
 StringUtils.indexOf("aabaabaa", 'b', 0)  = 2
 StringUtils.indexOf("aabaabaa", 'b', 3)  = 5
 StringUtils.indexOf("aabaabaa", 'b', 9)  = -1
 StringUtils.indexOf("aabaabaa", 'b', -1) = 2
 

Parameters:
str - the String to check, may be null
searchChar - the character to find
startPos - the start position, negative treated as zero
Returns:
the first index of the search character, -1 if no match or null string input
Since:
2.0

indexOf

public static int indexOf(java.lang.String str,
                          java.lang.String searchStr)

Finds the first index within a String, handling null. This method uses String.indexOf(String).

A null String will return -1.

 StringUtils.indexOf(null, *)          = -1
 StringUtils.indexOf(*, null)          = -1
 StringUtils.indexOf("", "")           = 0
 StringUtils.indexOf("aabaabaa", "a")  = 0
 StringUtils.indexOf("aabaabaa", "b")  = 2
 StringUtils.indexOf("aabaabaa", "ab") = 1
 StringUtils.indexOf("aabaabaa", "")   = 0
 

Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
Returns:
the first index of the search String, -1 if no match or null string input
Since:
2.0

ordinalIndexOf

public static int ordinalIndexOf(java.lang.String str,
                                 java.lang.String searchStr,
                                 int ordinal)

Finds the n-th index within a String, handling null. This method uses String.indexOf(String).

A null String will return -1.

 StringUtils.ordinalIndexOf(null, *, *)          = -1
 StringUtils.ordinalIndexOf(*, null, *)          = -1
 StringUtils.ordinalIndexOf("", "", *)           = 0
 StringUtils.ordinalIndexOf("aabaabaa", "a", 1)  = 0
 StringUtils.ordinalIndexOf("aabaabaa", "a", 2)  = 1
 StringUtils.ordinalIndexOf("aabaabaa", "b", 1)  = 2
 StringUtils.ordinalIndexOf("aabaabaa", "b", 2)  = 5
 StringUtils.ordinalIndexOf("aabaabaa", "ab", 1) = 1
 StringUtils.ordinalIndexOf("aabaabaa", "ab", 2) = 4
 StringUtils.ordinalIndexOf("aabaabaa", "", 1)   = 0
 StringUtils.ordinalIndexOf("aabaabaa", "", 2)   = 0
 

Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
ordinal - the n-th searchStr to find
Returns:
the n-th index of the search String, -1 (INDEX_NOT_FOUND) if no match or null string input
Since:
2.1

indexOf

public static int indexOf(java.lang.String str,
                          java.lang.String searchStr,
                          int startPos)

Finds the first index within a String, handling null. This method uses String.indexOf(String, int).

A null String will return -1. A negative start position is treated as zero. An empty ("") search String always matches. A start position greater than the string length only matches an empty search String.

 StringUtils.indexOf(null, *, *)          = -1
 StringUtils.indexOf(*, null, *)          = -1
 StringUtils.indexOf("", "", 0)           = 0
 StringUtils.indexOf("aabaabaa", "a", 0)  = 0
 StringUtils.indexOf("aabaabaa", "b", 0)  = 2
 StringUtils.indexOf("aabaabaa", "ab", 0) = 1
 StringUtils.indexOf("aabaabaa", "b", 3)  = 5
 StringUtils.indexOf("aabaabaa", "b", 9)  = -1
 StringUtils.indexOf("aabaabaa", "b", -1) = 2
 StringUtils.indexOf("aabaabaa", "", 2)   = 2
 StringUtils.indexOf("abc", "", 9)        = 3
 

Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
startPos - the start position, negative treated as zero
Returns:
the first index of the search String, -1 if no match or null string input
Since:
2.0

lastIndexOf

public static int lastIndexOf(java.lang.String str,
                              char searchChar)

Finds the last index within a String, handling null. This method uses String.lastIndexOf(int).

A null or empty ("") String will return -1.

 StringUtils.lastIndexOf(null, *)         = -1
 StringUtils.lastIndexOf("", *)           = -1
 StringUtils.lastIndexOf("aabaabaa", 'a') = 7
 StringUtils.lastIndexOf("aabaabaa", 'b') = 5
 

Parameters:
str - the String to check, may be null
searchChar - the character to find
Returns:
the last index of the search character, -1 if no match or null string input
Since:
2.0

lastIndexOf

public static int lastIndexOf(java.lang.String str,
                              char searchChar,
                              int startPos)

Finds the last index within a String from a start position, handling null. This method uses String.lastIndexOf(int, int).

A null or empty ("") String will return -1. A negative start position returns -1. A start position greater than the string length searches the whole string.

 StringUtils.lastIndexOf(null, *, *)          = -1
 StringUtils.lastIndexOf("", *,  *)           = -1
 StringUtils.lastIndexOf("aabaabaa", 'b', 8)  = 5
 StringUtils.lastIndexOf("aabaabaa", 'b', 4)  = 2
 StringUtils.lastIndexOf("aabaabaa", 'b', 0)  = -1
 StringUtils.lastIndexOf("aabaabaa", 'b', 9)  = 5
 StringUtils.lastIndexOf("aabaabaa", 'b', -1) = -1
 StringUtils.lastIndexOf("aabaabaa", 'a', 0)  = 0
 

Parameters:
str - the String to check, may be null
searchChar - the character to find
startPos - the start position
Returns:
the last index of the search character, -1 if no match or null string input
Since:
2.0

lastIndexOf

public static int lastIndexOf(java.lang.String str,
                              java.lang.String searchStr)

Finds the last index within a String, handling null. This method uses String.lastIndexOf(String).

A null String will return -1.

 StringUtils.lastIndexOf(null, *)          = -1
 StringUtils.lastIndexOf(*, null)          = -1
 StringUtils.lastIndexOf("", "")           = 0
 StringUtils.lastIndexOf("aabaabaa", "a")  = 0
 StringUtils.lastIndexOf("aabaabaa", "b")  = 2
 StringUtils.lastIndexOf("aabaabaa", "ab") = 1
 StringUtils.lastIndexOf("aabaabaa", "")   = 8
 

Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
Returns:
the last index of the search String, -1 if no match or null string input
Since:
2.0

lastIndexOf

public static int lastIndexOf(java.lang.String str,
                              java.lang.String searchStr,
                              int startPos)

Finds the first index within a String, handling null. This method uses String.lastIndexOf(String, int).

A null String will return -1. A negative start position returns -1. An empty ("") search String always matches unless the start position is negative. A start position greater than the string length searches the whole string.

 StringUtils.lastIndexOf(null, *, *)          = -1
 StringUtils.lastIndexOf(*, null, *)          = -1
 StringUtils.lastIndexOf("aabaabaa", "a", 8)  = 7
 StringUtils.lastIndexOf("aabaabaa", "b", 8)  = 5
 StringUtils.lastIndexOf("aabaabaa", "ab", 8) = 4
 StringUtils.lastIndexOf("aabaabaa", "b", 9)  = 5
 StringUtils.lastIndexOf("aabaabaa", "b", -1) = -1
 StringUtils.lastIndexOf("aabaabaa", "a", 0)  = 0
 StringUtils.lastIndexOf("aabaabaa", "b", 0)  = -1
 

Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
startPos - the start position, negative treated as zero
Returns:
the first index of the search String, -1 if no match or null string input
Since:
2.0

contains

public static boolean contains(java.lang.String str,
                               char searchChar)

Checks if String contains a search character, handling null. This method uses String.indexOf(int).

A null or empty ("") String will return false.

 StringUtils.contains(null, *)    = false
 StringUtils.contains("", *)      = false
 StringUtils.contains("abc", 'a') = true
 StringUtils.contains("abc", 'z') = false
 

Parameters:
str - the String to check, may be null
searchChar - the character to find
Returns:
true if the String contains the search character, false if not or null string input
Since:
2.0

contains

public static boolean contains(java.lang.String str,
                               java.lang.String searchStr)

Checks if String contains a search String, handling null. This method uses String.indexOf(String).

A null String will return false.

 StringUtils.contains(null, *)     = false
 StringUtils.contains(*, null)     = false
 StringUtils.contains("", "")      = true
 StringUtils.contains("abc", "")   = true
 StringUtils.contains("abc", "a")  = true
 StringUtils.contains("abc", "z")  = false
 

Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
Returns:
true if the String contains the search String, false if not or null string input
Since:
2.0

containsIgnoreCase

public static boolean containsIgnoreCase(java.lang.String str,
                                         java.lang.String searchStr)

Checks if String contains a search String irrespective of case, handling null. This method uses contains(String, String).

A null String will return false.

 StringUtils.contains(null, *) = false
 StringUtils.contains(*, null) = false
 StringUtils.contains("", "") = true
 StringUtils.contains("abc", "") = true
 StringUtils.contains("abc", "a") = true
 StringUtils.contains("abc", "z") = false
 StringUtils.contains("abc", "A") = true
 StringUtils.contains("abc", "Z") = false
 

Parameters:
str - the String to check, may be null
searchStr - the String to find, may be null
Returns:
true if the String contains the search String irrespective of case or false if not or null string input

indexOfAny

public static int indexOfAny(java.lang.String str,
                             char[] searchChars)

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

A null String will return -1. A null or zero length search array will return -1.

 StringUtils.indexOfAny(null, *)                = -1
 StringUtils.indexOfAny("", *)                  = -1
 StringUtils.indexOfAny(*, null)                = -1
 StringUtils.indexOfAny(*, [])                  = -1
 StringUtils.indexOfAny("zzabyycdxx",['z','a']) = 0
 StringUtils.indexOfAny("zzabyycdxx",['b','y']) = 3
 StringUtils.indexOfAny("aba", ['z'])           = -1
 

Parameters:
str - the String to check, may be null
searchChars - the chars to search for, may be null
Returns:
the index of any of the chars, -1 if no match or null input
Since:
2.0

indexOfAny

public static int indexOfAny(java.lang.String str,
                             java.lang.String searchChars)

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

A null String will return -1. A null search string will return -1.

 StringUtils.indexOfAny(null, *)            = -1
 StringUtils.indexOfAny("", *)              = -1
 StringUtils.indexOfAny(*, null)            = -1
 StringUtils.indexOfAny(*, "")              = -1
 StringUtils.indexOfAny("zzabyycdxx", "za") = 0
 StringUtils.indexOfAny("zzabyycdxx", "by") = 3
 StringUtils.indexOfAny("aba","z")          = -1
 

Parameters:
str - the String to check, may be null
searchChars - the chars to search for, may be null
Returns:
the index of any of the chars, -1 if no match or null input
Since:
2.0

containsAny

public static boolean containsAny(java.lang.String str,
                                  char[] searchChars)

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

A null String will return false. A null or zero length search array will return false