|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectorg.apache.commons.lang.StringUtils
Operations on String that are
null safe.
The StringUtils class defines certain words related to
String handling.
null"")' ', char 32)Character.isWhitespace(char)String.trim()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.
String| Field Summary | |
static String |
EMPTY
The empty String "". |
| Constructor Summary | |
StringUtils()
StringUtils instances should NOT be constructed in
standard programming. |
|
| Method Summary | |
static String |
abbreviate(String str,
int maxWidth)
Abbreviates a String using ellipses. |
static String |
abbreviate(String str,
int offset,
int maxWidth)
Abbreviates a String using ellipses. |
static String |
capitalise(String str)
Deprecated. Use the standardly named capitalize(String).
Method will be removed in Commons Lang 3.0. |
static String |
capitaliseAllWords(String str)
Deprecated. Use the relocated WordUtils.capitalize(String).
Method will be removed in Commons Lang 3.0. |
static String |
capitalize(String str)
Capitalizes a String changing the first letter to title case as per Character.toTitleCase(char). |
static String |
center(String str,
int size)
Centers a String in a larger String of size size
using the space character (' '). |
static String |
center(String str,
int size,
char padChar)
Centers a String in a larger String of size size. |
static String |
center(String str,
int size,
String padStr)
Centers a String in a larger String of size size. |
static String |
chomp(String str)
Removes one newline from end of a String if it's there, otherwise leave it alone. |
static String |
chomp(String str,
String separator)
Removes separator from the end of
str if it's there, otherwise leave it alone. |
static String |
chompLast(String str)
Deprecated. Use chomp(String) instead.
Method will be removed in Commons Lang 3.0. |
static String |
chompLast(String str,
String sep)
Deprecated. Use chomp(String,String) instead.
Method will be removed in Commons Lang 3.0. |
static String |
chop(String str)
Remove the last character from a String. |
static String |
chopNewline(String str)
Deprecated. Use chomp(String) instead.
Method will be removed in Commons Lang 3.0. |
static String |
clean(String str)
Deprecated. Use the clearer named trimToEmpty(String).
Method will be removed in Commons Lang 3.0. |
static String |
concatenate(Object[] array)
Deprecated. Use the better named join(Object[]) instead.
Method will be removed in Commons Lang 3.0. |
static boolean |
contains(String str,
char searchChar)
Checks if String contains a search character, handling null. |
static boolean |
contains(String str,
String searchStr)
Find the first index within a String, handling null. |
static boolean |
containsNone(String str,
char[] invalidChars)
Checks that the String does not contain certain characters. |
static boolean |
containsNone(String str,
String invalidChars)
Checks that the String does not contain certain characters. |
static boolean |
containsOnly(String str,
char[] valid)
Checks if the String contains only certain characters. |
static boolean |
containsOnly(String str,
String validChars)
Checks if the String contains only certain characters. |
static int |
countMatches(String str,
String sub)
Counts how many times the substring appears in the larger String. |
static String |
defaultString(String str)
Returns either the passed in String, or if the String is null, an empty String (""). |
static String |
defaultString(String str,
String defaultStr)
Returns either the passed in String, or if the String is null, an empty String (""). |
static String |
deleteSpaces(String str)
Deprecated. Use the better localized deleteWhitespace(String).
Method will be removed in Commons Lang 3.0. |
static String |
deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by Character.isWhitespace(char). |
static String |
difference(String str1,
String str2)
Compares two Strings, and returns the portion where they differ. |
static boolean |
equals(String str1,
String str2)
Compares two Strings, returning true if they are equal. |
static boolean |
equalsIgnoreCase(String str1,
String str2)
Compares two Strings, returning true if they are equal ignoring
the case. |
static String |
escape(String str)
Deprecated. Use StringEscapeUtils.escapeJava(String)
This method will be removed in Commons Lang 3.0 |
static String |
getChomp(String str,
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 int |
getLevenshteinDistance(String s,
String t)
Find the Levenshtein distance between two Strings. |
static String |
getNestedString(String str,
String tag)
Deprecated. Use the better named substringBetween(String, String).
Method will be removed in Commons Lang 3.0. |
static String |
getNestedString(String str,
String open,
String close)
Deprecated. Use the better named substringBetween(String, String, String).
Method will be removed in Commons Lang 3.0. |
static String |
getPrechomp(String str,
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(String str,
char searchChar)
Finds the first index within a String, handling null. |
static int |
indexOf(String str,
char searchChar,
int startPos)
Finds the first index within a String from a start position, handling null. |
static int |
indexOf(String str,
String searchStr)
Finds the first index within a String, handling null. |
static int |
indexOf(String str,
String searchStr,
int startPos)
Finds the first index within a String, handling null. |
static int |
indexOfAny(String str,
char[] searchChars)
Search a String to find the first index of any character in the given set of characters. |
static int |
indexOfAny(String str,
String searchChars)
Search a String to find the first index of any character in the given set of characters. |
static int |
indexOfAny(String str,
String[] searchStrs)
Find the first index of any of a set of potential substrings. |
static int |
indexOfAnyBut(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(String str,
String searchChars)
Search a String to find the first index of any character not in the given set of characters. |
static int |
indexOfDifference(String str1,
String str2)
Compares two Strings, and returns the index at which the Strings begin to differ. |
static boolean |
isAlpha(String str)
Checks if the String contains only unicode letters. |
static boolean |
isAlphanumeric(String str)
Checks if the String contains only unicode letters or digits. |
static boolean |
isAlphanumericSpace(String str)
Checks if the String contains only unicode letters, digits or space ( ' '). |
static boolean |
isAlphaSpace(String str)
Checks if the String contains only unicode letters and space (' '). |
static boolean |
isBlank(String str)
Checks if a String is whitespace, empty ("") or null. |
static boolean |
isEmpty(String str)
Checks if a String is empty ("") or null. |
static boolean |
isNotBlank(String str)
Checks if a String is not empty (""), not null and not whitespace only. |
static boolean |
isNotEmpty(String str)
Checks if a String is not empty ("") and not null. |
static boolean |
isNumeric(String str)
Checks if the String contains only unicode digits. |
static boolean |
isNumericSpace(String str)
Checks if the String contains only unicode digits or space ( ' '). |
static boolean |
isWhitespace(String str)
Checks if the String contains only whitespace. |
static String |
join(Iterator iterator,
char separator)
Joins the elements of the provided Iterator into
a single String containing the provided elements. |
static String |
join(Iterator iterator,
String separator)
Joins the elements of the provided Iterator into
a single String containing the provided elements. |
static String |
join(Object[] array)
Joins the elements of the provided array into a single String containing the provided list of elements. |
static String |
join(Object[] array,
char separator)
Joins the elements of the provided array into a single String containing the provided list of elements. |
static String |
join(Object[] array,
String separator)
Joins the elements of the provided array into a single String containing the provided list of elements. |
static int |
lastIndexOf(String str,
char searchChar)
Finds the last index within a String, handling null. |
static int |
lastIndexOf(String str,
char searchChar,
int startPos)
Finds the last index within a String from a start position, handling null. |
static int |
lastIndexOf(String str,
String searchStr)
Finds the last index within a String, handling null. |
static int |
lastIndexOf(String str,
String searchStr,
int startPos)
Finds the first index within a String, handling null. |
static int |
lastIndexOfAny(String str,
String[] searchStrs)
Find the latest index of any of a set of potential substrings. |
static String |
left(String str,
int len)
Gets the leftmost len characters of a String. |
static String |
leftPad(String str,
int size)
Left pad a String with spaces (' '). |
static String |
leftPad(String str,
int size,
char padChar)
Left pad a String with a specified character. |
static String |
leftPad(String str,
int size,
String padStr)
Left pad a String with a specified String. |
static String |
lowerCase(String str)
Converts a String to lower case as per String.toLowerCase(). |
static String |
mid(String str,
int pos,
int len)
Gets len characters from the middle of a String. |
static String |
overlay(String str,
String overlay,
int start,
int end)
Overlays part of a String with another String. |
static String |
overlayString(String text,
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 String |
prechomp(String str,
String sep)
Deprecated. Use substringAfter(String,String) instead.
Method will be removed in Commons Lang 3.0. |
static String |
repeat(String str,
int repeat)
Repeat a String repeat times to form a
new String. |
static String |
replace(String text,
String repl,
String with)
Replaces all occurances of a String within another String. |
static String |
replace(String text,
String repl,
String with,
int max)
Replaces a String with another String inside a larger String, for the first max values of the search String. |
static String |
replaceChars(String str,
char searchChar,
char replaceChar)
Replaces all occurrances of a character in a String with another. |
static String |
replaceChars(String str,
String searchChars,
String replaceChars)
Replaces multiple characters in a String in one go. |
static String |
replaceOnce(String text,
String repl,
String with)
Replaces a String with another String inside a larger String, once. |
static String |
reverse(String str)
Reverses a String as per StringBuffer.reverse(). |
static String |
reverseDelimited(String str,
char separatorChar)
Reverses a String that is delimited by a specific character. |
static String |
reverseDelimitedString(String str,
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 String |
right(String str,
int len)
Gets the rightmost len characters of a String. |
static String |
rightPad(String str,
int size)
Right pad a String with spaces (' '). |
static String |
rightPad(String str,
int size,
char padChar)
Right pad a String with a specified character. |
static String |
rightPad(String str,
int size,
String padStr)
Right pad a String with a specified String. |
static String[] |
split(String str)
Splits the provided text into an array, using whitespace as the separator. |
static String[] |
split(String str,
char separatorChar)
Splits the provided text into an array, separator specified. |
static String[] |
split(String str,
String separatorChars)
Splits the provided text into an array, separators specified. |
static String[] |
split(String str,
String separatorChars,
int max)
Splits the provided text into an array, separators specified. |
static String |
strip(String str)
Strips whitespace from the start and end of a String. |
static String |
strip(String str,
String stripChars)
Strips any of a set of characters from the start and end of a String. |
static String[] |
stripAll(String[] strs)
Strips whitespace from the start and end of every String in an array. |
static String[] |
stripAll(String[] strs,
String stripChars)
Strips any of a set of characters from the start and end of every String in an array. |
static String |
stripEnd(String str,
String stripChars)
Strips any of a set of characters from the end of a String. |
static String |
stripStart(String str,
String stripChars)
Strips any of a set of characters from the start of a String. |
static String |
stripToEmpty(String str)
Strips whitespace from the start and end of a String returning an empty String if null input. |
static String |
stripToNull(String str)
Strips whitespace from the start and end of a String returning null if the String is empty ("") after the strip. |
static String |
substring(String str,
int start)
Gets a substring from the specified String avoiding exceptions. |
static String |
substring(String str,
int start,
int end)
Gets a substring from the specified String avoiding exceptions. |
static String |
substringAfter(String str,
String separator)
Gets the substring after the first occurance of a separator. |
static String |
substringAfterLast(String str,
String separator)
Gets the substring after the last occurance of a separator. |
static String |
substringBefore(String str,
String separator)
Gets the substring before the first occurance of a separator. |
static String |
substringBeforeLast(String str,
String separator)
Gets the substring before the last occurance of a separator. |
static String |
substringBetween(String str,
String tag)
Gets the String that is nested in between two instances of the same String. |
static String |
substringBetween(String str,
String open,
String close)
Gets the String that is nested in between two Strings. |
static String |
swapCase(String str)
Swaps the case of a String changing upper and title case to lower case, and lower case to upper case. |
static String |
trim(String str)
Removes control characters (char <= 32) from both ends of this String, handling null by returning
null. |
static String |
trimToEmpty(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 String |
trimToNull(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 String |
uncapitalise(String str)
Deprecated. Use the standardly named uncapitalize(String).
Method will be removed in Commons Lang 3.0. |
static String |
uncapitalize(String str)
Uncapitalizes a String changing the first letter to title case as per Character.toLowerCase(char). |
static String |
upperCase(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 |
public static final String EMPTY
"".
| Constructor Detail |
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 |
public static boolean isEmpty(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().
str - the String to check, may be null
true if the String is empty or nullpublic static boolean isNotEmpty(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
str - the String to check, may be null
true if the String is not empty and not nullpublic static boolean isBlank(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
str - the String to check, may be null
true if the String is null, empty or whitespacepublic static boolean isNotBlank(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
str - the String to check, may be null
true if the String is
not empty and not null and not whitespacepublic static String clean(String str)
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(" ") = ""
str - the String to clean, may be null
nullString.trim()public static String trim(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"
str - the String to be trimmed, may be null
null if null String inputpublic static String trimToNull(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"
str - the String to be trimmed, may be null
null if only chars <= 32, empty or null String inputpublic static String trimToEmpty(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"
str - the String to be trimmed, may be null
null inputpublic static String strip(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"
str - the String to remove whitespace from, may be null
null if null String inputpublic static String stripToNull(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.strip(null) = null
StringUtils.strip("") = null
StringUtils.strip(" ") = null
StringUtils.strip("abc") = "abc"
StringUtils.strip(" abc") = "abc"
StringUtils.strip("abc ") = "abc"
StringUtils.strip(" abc ") = "abc"
StringUtils.strip(" ab c ") = "ab c"
str - the String to be stripped, may be null
null if whitespace, empty or null String inputpublic static String stripToEmpty(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.strip(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"
str - the String to be stripped, may be null
null input
public static String strip(String str,
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"
str - the String to remove characters from, may be nullstripChars - the characters to remove, null treated as whitespace
null if null String input
public static String stripStart(String str,
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 "
str - the String to remove characters from, may be nullstripChars - the characters to remove, null treated as whitespace
null if null String input
public static String stripEnd(String str,
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"
str - the String to remove characters from, may be nullstripChars - the characters to remove, null treated as whitespace
null if null String inputpublic static String[] stripAll(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]
strs - the array to remove whitespace from, may be null
null if null array input
public static String[] stripAll(String[] strs,
String stripChars)
Strips any of a set of characters from the start and end of every String in an array.
Whitespace is defined byCharacter.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]
strs - the array to remove characters from, may be nullstripChars - the characters to remove, null treated as whitespace
null if null array input
public static boolean equals(String str1,
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
str1 - the first String, may be nullstr2 - the second String, may be null
true if the Strings are equal, case sensitive, or
both nullString.equals(Object)
public static boolean equalsIgnoreCase(String str1,
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
str1 - the first String, may be nullstr2 - the second String, may be null
true if the Strings are equal, case insensitive, or
both nullString.equalsIgnoreCase(String)
public static int indexOf(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
str - the String