public class ManStringUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String |
EMPTY
The empty String
"" . |
static int |
INDEX_NOT_FOUND
Represents a failed index search.
|
Constructor and Description |
---|
ManStringUtil() |
Modifier and Type | Method and Description |
---|---|
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 |
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)
Checks if String contains a search String, handling
null . |
static boolean |
containsAny(String str,
char[] searchChars)
Checks if the String contains any character in the given
set of characters.
|
static boolean |
containsAny(String str,
String searchChars)
Checks if the String contains any character in the given set of characters.
|
static boolean |
containsIgnoreCase(String str,
String searchStr)
Checks if String contains a search String irrespective of case,
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 int |
countRegexpMatches(String str,
String regexp)
Counts how many times the regexp appears in the larger String.
|
static String |
defaultIfEmpty(String str,
String defaultStr)
Returns either the passed in String, or if the String is
empty or
null , the value of defaultStr . |
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 , the value of defaultStr . |
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 |
endsWith(String str,
String suffix)
Check if a String ends with a specified suffix.
|
static boolean |
endsWithIgnoreCase(String str,
String suffix)
Case insensitive check if a String ends with a specified suffix.
|
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 ArrayList<Integer> |
findDistinctIndexesOf(String string,
String substringToLookFor) |
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 String |
getCommonPrefix(String[] strs)
Compares all Strings in an array and returns the demo sequence of
characters that is common to all of them.
|
static int |
getIndexForLineNumber(String strSource,
int iLine) |
static int |
getLevenshteinDistance(String s,
String t)
Find the Levenshtein distance between two Strings.
|
static int |
getLineNumberForIndex(String strSource,
int iIndex) |
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 String |
getSHA1String(String s) |
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[] strs)
Compares all Strings in an array and returns the index at which the
Strings begin to differ.
|
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 |
isAsciiAlphanumeric(char ch)
Checks whether the character is ASCII 7 bit numeric.
|
static boolean |
isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
|
static boolean |
isCrLf(String str) |
static boolean |
isEmpty(String str)
Checks if a String is empty ("") or null.
|
static boolean |
isHexidecimal(String str)
Checks if the String contains only hexidecimal digits.
|
static boolean |
isHexidecimalSpace(String str)
Checks if the String contains only hexidecimal digits or space
(
' ' ). |
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(Collection collection,
char separator)
Joins the elements of the provided
Collection into
a single String containing the provided elements. |
static String |
join(Collection collection,
String separator)
Joins the elements of the provided
Collection into
a single String containing the provided elements. |
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,
char separator,
int startIndex,
int endIndex)
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 String |
join(Object[] array,
String separator,
int startIndex,
int endIndex)
Joins the elements of the provided array into a single String
containing the provided list of elements.
|
static String |
join(String glue,
Collection charSequences) |
static String |
join(String glue,
Object[] charSequences) |
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 int |
length(String str)
Gets a String's length or
0 if the String is null . |
static String |
lowerCase(String str)
Converts a String to lower case as per
String.toLowerCase() . |
static String |
lowerCase(String str,
Locale locale)
Converts a String to lower case as per
String.toLowerCase(Locale) . |
static RegExpMatch |
match(String str,
String regExp) |
static String |
mid(String str,
int pos,
int len)
Gets
len characters from the middle of a String. |
static int |
ordinalIndexOf(String str,
String searchStr,
int ordinal)
Finds the n-th index within a String, handling
null . |
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 |
remove(String str,
char remove)
Removes all occurrences of a character from within the source string.
|
static String |
remove(String str,
String remove)
Removes all occurrences of a substring from within the source string.
|
static String |
removeEnd(String str,
String remove)
Removes a substring only if it is at the end of a source string,
otherwise returns the source string.
|
static String |
removeEndIgnoreCase(String str,
String remove)
Case insensitive removal of a substring if it is at the end of a source string,
otherwise returns the source string.
|
static String |
removeStart(String str,
String remove)
Removes a substring only if it is at the begining of a source string,
otherwise returns the source string.
|
static String |
removeStartIgnoreCase(String str,
String remove)
Case insensitive removal of a substring if it is at the begining of a source string,
otherwise returns the source string.
|
static String |
repeat(String str,
int repeat)
Repeat a String
repeat times to form a
new String. |
static String |
replace(String text,
String searchString,
String replacement)
Replaces all occurrences of a String within another String.
|
static String |
replace(String text,
String searchString,
String replacement,
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 occurrences 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 |
replaceCRLFwithLF(String text) |
static String |
replaceEach(String text,
String[] searchList,
String[] replacementList)
Replaces all occurrences of Strings within another String.
|
static String |
replaceEachRepeatedly(String text,
String[] searchList,
String[] replacementList)
Replaces all occurrences of Strings within another String.
|
static String |
replaceOnce(String text,
String searchString,
String replacement)
Replaces a String with another String inside a larger String, once.
|
static String |
reverse(String str)
Reverses a String as per
StringBuilder.reverse() . |
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 with a maximum length,
separators specified.
|
static String[] |
splitByCharacterType(String str)
Splits a String by Character type as returned by
java.lang.Character.getType(char) . |
static String[] |
splitByCharacterTypeCamelCase(String str)
Splits a String by Character type as returned by
java.lang.Character.getType(char) . |
static String[] |
splitByWholeSeparator(String str,
String separator)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitByWholeSeparator(String str,
String separator,
int max)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitByWholeSeparatorPreserveAllTokens(String str,
String separator)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitByWholeSeparatorPreserveAllTokens(String str,
String separator,
int max)
Splits the provided text into an array, separator string specified.
|
static String[] |
splitPreserveAllTokens(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 String[] |
splitPreserveAllTokens(String str,
char separatorChar)
Splits the provided text into an array, separator specified,
preserving all tokens, including empty tokens created by adjacent
separators.
|
static String[] |
splitPreserveAllTokens(String str,
String separatorChars)
Splits the provided text into an array, separators specified,
preserving all tokens, including empty tokens created by adjacent
separators.
|
static String[] |
splitPreserveAllTokens(String str,
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(String str,
String prefix)
Check if a String starts with a specified prefix.
|
static boolean |
startsWithAny(String string,
String[] searchStrings)
Check if a String starts with any of an array of specified strings.
|
static boolean |
startsWithIgnoreCase(String str,
String prefix)
Case insensitive check if a String starts with a specified prefix.
|
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 occurrence of a separator.
|
static String |
substringAfterLast(String str,
String separator)
Gets the substring after the last occurrence of a separator.
|
static String |
substringBefore(String str,
String separator)
Gets the substring before the first occurrence of a separator.
|
static String |
substringBeforeLast(String str,
String separator)
Gets the substring before the last occurrence 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[] |
substringsBetween(String str,
String open,
String close)
Searches a String for substrings delimited by a start and end tag,
returning all matching substrings in an array.
|
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 |
toCamelCase(String thiz)
Converts snake_case/kebab-case to camelCase e.g., my_var_name becomes myVarName.
|
static String[] |
tokenize(String s,
char separator)
Split up a string into tokens delimited by the specified separator
character.
|
static String |
toPascalCase(String thiz,
boolean lowerFirst)
Converts snake_case/kebab-case to PascalCase e.g., my-type-name becomes MyTypeName.
|
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 |
unquote(String text) |
static String |
upperCase(String str)
Converts a String to upper case as per
String.toUpperCase() . |
static String |
upperCase(String str,
Locale locale)
Converts a String to upper case as per
String.toUpperCase(Locale) . |
public static final String EMPTY
""
.public static final int INDEX_NOT_FOUND
public static String join(String glue, Collection charSequences)
public static ArrayList<Integer> findDistinctIndexesOf(String string, String substringToLookFor)
public static String[] tokenize(String s, char separator)
s
- The String to tokenizeseparator
- The character delimiting tokenspublic 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 nulltrue
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 nulltrue
if the String is not empty and not nullpublic static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
ManStringUtil.isBlank(null) = true ManStringUtil.isBlank("") = true ManStringUtil.isBlank(" ") = true ManStringUtil.isBlank("bob") = false ManStringUtil.isBlank(" bob ") = false
str
- the String to check, may be nulltrue
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.
ManStringUtil.isNotBlank(null) = false ManStringUtil.isNotBlank("") = false ManStringUtil.isNotBlank(" ") = false ManStringUtil.isNotBlank("bob") = true ManStringUtil.isNotBlank(" bob ") = true
str
- the String to check, may be nulltrue
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 ("").
ManStringUtil.clean(null) = "" ManStringUtil.clean("") = "" ManStringUtil.clean("abc") = "abc" ManStringUtil.clean(" abc ") = "abc" ManStringUtil.clean(" ") = ""
str
- the String to clean, may be nullnull
String.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.
ManStringUtil.trim(null) = null ManStringUtil.trim("") = "" ManStringUtil.trim(" ") = "" ManStringUtil.trim("abc") = "abc" ManStringUtil.trim(" abc ") = "abc"
str
- the String to be trimmed, may be nullnull
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)
.
ManStringUtil.trimToNull(null) = null ManStringUtil.trimToNull("") = null ManStringUtil.trimToNull(" ") = null ManStringUtil.trimToNull("abc") = "abc" ManStringUtil.trimToNull(" abc ") = "abc"
str
- the String to be trimmed, may be nullnull
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)
.
ManStringUtil.trimToEmpty(null) = "" ManStringUtil.trimToEmpty("") = "" ManStringUtil.trimToEmpty(" ") = "" ManStringUtil.trimToEmpty("abc") = "abc" ManStringUtil.trimToEmpty(" abc ") = "abc"
str
- the String to be trimmed, may be nullnull
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
.
ManStringUtil.strip(null) = null ManStringUtil.strip("") = "" ManStringUtil.strip(" ") = "" ManStringUtil.strip("abc") = "abc" ManStringUtil.strip(" abc") = "abc" ManStringUtil.strip("abc ") = "abc" ManStringUtil.strip(" abc ") = "abc" ManStringUtil.strip(" ab c ") = "ab c"
str
- the String to remove whitespace from, may be nullnull
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)
.
ManStringUtil.stripToNull(null) = null ManStringUtil.stripToNull("") = null ManStringUtil.stripToNull(" ") = null ManStringUtil.stripToNull("abc") = "abc" ManStringUtil.stripToNull(" abc") = "abc" ManStringUtil.stripToNull("abc ") = "abc" ManStringUtil.stripToNull(" abc ") = "abc" ManStringUtil.stripToNull(" ab c ") = "ab c"
str
- the String to be stripped, may be nullnull
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)
.
ManStringUtil.stripToEmpty(null) = "" ManStringUtil.stripToEmpty("") = "" ManStringUtil.stripToEmpty(" ") = "" ManStringUtil.stripToEmpty("abc") = "abc" ManStringUtil.stripToEmpty(" abc") = "abc" ManStringUtil.stripToEmpty("abc ") = "abc" ManStringUtil.stripToEmpty(" abc ") = "abc" ManStringUtil.stripToEmpty(" ab c ") = "ab c"
str
- the String to be stripped, may be nullnull
inputpublic 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)
.
ManStringUtil.strip(null, *) = null ManStringUtil.strip("", *) = "" ManStringUtil.strip("abc", null) = "abc" ManStringUtil.strip(" abc", null) = "abc" ManStringUtil.strip("abc ", null) = "abc" ManStringUtil.strip(" abc ", null) = "abc" ManStringUtil.strip(" abcyx", "xyz") = " abc"
str
- the String to remove characters from, may be nullstripChars
- the characters to remove, null treated as whitespacenull
if null String inputpublic 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)
.
ManStringUtil.stripStart(null, *) = null ManStringUtil.stripStart("", *) = "" ManStringUtil.stripStart("abc", "") = "abc" ManStringUtil.stripStart("abc", null) = "abc" ManStringUtil.stripStart(" abc", null) = "abc" ManStringUtil.stripStart("abc ", null) = "abc " ManStringUtil.stripStart(" abc ", null) = "abc " ManStringUtil.stripStart("yxabc ", "xyz") = "abc "
str
- the String to remove characters from, may be nullstripChars
- the characters to remove, null treated as whitespacenull
if null String inputpublic 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)
.
ManStringUtil.stripEnd(null, *) = null ManStringUtil.stripEnd("", *) = "" ManStringUtil.stripEnd("abc", "") = "abc" ManStringUtil.stripEnd("abc", null) = "abc" ManStringUtil.stripEnd(" abc", null) = " abc" ManStringUtil.stripEnd("abc ", null) = "abc" ManStringUtil.stripEnd(" abc ", null) = " abc" ManStringUtil.stripEnd(" abcyx", "xyz") = " abc"
str
- the String to remove characters from, may be nullstripChars
- the characters to remove, null treated as whitespacenull
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.
ManStringUtil.stripAll(null) = null ManStringUtil.stripAll([]) = [] ManStringUtil.stripAll(["abc", " abc"]) = ["abc", "abc"] ManStringUtil.stripAll(["abc ", null]) = ["abc", null]
strs
- the array to remove whitespace from, may be nullnull
if null array inputpublic 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)
.
ManStringUtil.stripAll(null, *) = null ManStringUtil.stripAll([], *) = [] ManStringUtil.stripAll(["abc", " abc"], null) = ["abc", "abc"] ManStringUtil.stripAll(["abc ", null], null) = ["abc", null] ManStringUtil.stripAll(["abc ", null], "yz") = ["abc ", null] ManStringUtil.stripAll(["yabcz", null], "yz") = ["abc", null]
strs
- the array to remove characters from, may be nullstripChars
- the characters to remove, null treated as whitespacenull
if null array inputpublic static boolean equals(String str1, String str2)
Compares two Strings, returning true
if they are equal.
null
s are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
ManStringUtil.equals(null, null) = true ManStringUtil.equals(null, "abc") = false ManStringUtil.equals("abc", null) = false ManStringUtil.equals("abc", "abc") = true ManStringUtil.equals("abc", "ABC") = false
str1
- the first String, may be nullstr2
- the second String, may be nulltrue
if the Strings are equal, case sensitive, or
both null
String.equals(Object)
public static boolean equalsIgnoreCase(String str1, String str2)
Compares two Strings, returning true
if they are equal ignoring
the case.
null
s are handled without exceptions. Two null
references are considered equal. Comparison is case insensitive.
ManStringUtil.equalsIgnoreCase(null, null) = true ManStringUtil.equalsIgnoreCase(null, "abc") = false ManStringUtil.equalsIgnoreCase("abc", null) = false ManStringUtil.equalsIgnoreCase("abc", "abc") = true ManStringUtil.equalsIgnoreCase("abc", "ABC") = true
str1
- the first String, may be nullstr2
- the second String, may be nulltrue
if the Strings are equal, case insensitive, or
both null
String.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
.
ManStringUtil.indexOf(null, *) = -1 ManStringUtil.indexOf("", *) = -1 ManStringUtil.indexOf("aabaabaa", 'a') = 0 ManStringUtil.indexOf("aabaabaa", 'b') = 2
str
- the String to check, may be nullsearchChar
- the character to findnull
string inputpublic static int indexOf(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
.
ManStringUtil.indexOf(null, *, *) = -1 ManStringUtil.indexOf("", *, *) = -1 ManStringUtil.indexOf("aabaabaa", 'b', 0) = 2 ManStringUtil.indexOf("aabaabaa", 'b', 3) = 5 ManStringUtil.indexOf("aabaabaa", 'b', 9) = -1 ManStringUtil.indexOf("aabaabaa", 'b', -1) = 2
str
- the String to check, may be nullsearchChar
- the character to findstartPos
- the start position, negative treated as zeronull
string inputpublic static int indexOf(String str, String searchStr)
Finds the first index within a String, handling null
.
This method uses String.indexOf(String)
.
A null
String will return -1
.
ManStringUtil.indexOf(null, *) = -1 ManStringUtil.indexOf(*, null) = -1 ManStringUtil.indexOf("", "") = 0 ManStringUtil.indexOf("aabaabaa", "a") = 0 ManStringUtil.indexOf("aabaabaa", "b") = 2 ManStringUtil.indexOf("aabaabaa", "ab") = 1 ManStringUtil.indexOf("aabaabaa", "") = 0
str
- the String to check, may be nullsearchStr
- the String to find, may be nullnull
string inputpublic static int ordinalIndexOf(String str, 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
.
ManStringUtil.ordinalIndexOf(null, *, *) = -1 ManStringUtil.ordinalIndexOf(*, null, *) = -1 ManStringUtil.ordinalIndexOf("", "", *) = 0 ManStringUtil.ordinalIndexOf("aabaabaa", "a", 1) = 0 ManStringUtil.ordinalIndexOf("aabaabaa", "a", 2) = 1 ManStringUtil.ordinalIndexOf("aabaabaa", "b", 1) = 2 ManStringUtil.ordinalIndexOf("aabaabaa", "b", 2) = 5 ManStringUtil.ordinalIndexOf("aabaabaa", "ab", 1) = 1 ManStringUtil.ordinalIndexOf("aabaabaa", "ab", 2) = 4 ManStringUtil.ordinalIndexOf("aabaabaa", "", 1) = 0 ManStringUtil.ordinalIndexOf("aabaabaa", "", 2) = 0
str
- the String to check, may be nullsearchStr
- the String to find, may be nullordinal
- the n-th searchStr
to find-1
(INDEX_NOT_FOUND
) if no match or null
string inputpublic static int indexOf(String str, 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.
ManStringUtil.indexOf(null, *, *) = -1 ManStringUtil.indexOf(*, null, *) = -1 ManStringUtil.indexOf("", "", 0) = 0 ManStringUtil.indexOf("aabaabaa", "a", 0) = 0 ManStringUtil.indexOf("aabaabaa", "b", 0) = 2 ManStringUtil.indexOf("aabaabaa", "ab", 0) = 1 ManStringUtil.indexOf("aabaabaa", "b", 3) = 5 ManStringUtil.indexOf("aabaabaa", "b", 9) = -1 ManStringUtil.indexOf("aabaabaa", "b", -1) = 2 ManStringUtil.indexOf("aabaabaa", "", 2) = 2 ManStringUtil.indexOf("abc", "", 9) = 3
str
- the String to check, may be nullsearchStr
- the String to find, may be nullstartPos
- the start position, negative treated as zeronull
string inputpublic static int lastIndexOf(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
.
ManStringUtil.lastIndexOf(null, *) = -1 ManStringUtil.lastIndexOf("", *) = -1 ManStringUtil.lastIndexOf("aabaabaa", 'a') = 7 ManStringUtil.lastIndexOf("aabaabaa", 'b') = 5
str
- the String to check, may be nullsearchChar
- the character to findnull
string inputpublic static int lastIndexOf(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.
ManStringUtil.lastIndexOf(null, *, *) = -1 ManStringUtil.lastIndexOf("", *, *) = -1 ManStringUtil.lastIndexOf("aabaabaa", 'b', 8) = 5 ManStringUtil.lastIndexOf("aabaabaa", 'b', 4) = 2 ManStringUtil.lastIndexOf("aabaabaa", 'b', 0) = -1 ManStringUtil.lastIndexOf("aabaabaa", 'b', 9) = 5 ManStringUtil.lastIndexOf("aabaabaa", 'b', -1) = -1 ManStringUtil.lastIndexOf("aabaabaa", 'a', 0) = 0
str
- the String to check, may be nullsearchChar
- the character to findstartPos
- the start positionnull
string inputpublic static int lastIndexOf(String str, String searchStr)
Finds the last index within a String, handling null
.
This method uses String.lastIndexOf(String)
.
A null
String will return -1
.
ManStringUtil.lastIndexOf(null, *) = -1 ManStringUtil.lastIndexOf(*, null) = -1 ManStringUtil.lastIndexOf("", "") = 0 ManStringUtil.lastIndexOf("aabaabaa", "a") = 0 ManStringUtil.lastIndexOf("aabaabaa", "b") = 2 ManStringUtil.lastIndexOf("aabaabaa", "ab") = 1 ManStringUtil.lastIndexOf("aabaabaa", "") = 8
str
- the String to check, may be nullsearchStr
- the String to find, may be nullnull
string inputpublic static int lastIndexOf(String str, 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.
ManStringUtil.lastIndexOf(null, *, *) = -1 ManStringUtil.lastIndexOf(*, null, *) = -1 ManStringUtil.lastIndexOf("aabaabaa", "a", 8) = 7 ManStringUtil.lastIndexOf("aabaabaa", "b", 8) = 5 ManStringUtil.lastIndexOf("aabaabaa", "ab", 8) = 4 ManStringUtil.lastIndexOf("aabaabaa", "b", 9) = 5 ManStringUtil.lastIndexOf("aabaabaa", "b", -1) = -1 ManStringUtil.lastIndexOf("aabaabaa", "a", 0) = 0 ManStringUtil.lastIndexOf("aabaabaa", "b", 0) = -1
str
- the String to check, may be nullsearchStr
- the String to find, may be nullstartPos
- the start position, negative treated as zeronull
string inputpublic static boolean contains(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
.
ManStringUtil.contains(null, *) = false ManStringUtil.contains("", *) = false ManStringUtil.contains("abc", 'a') = true ManStringUtil.contains("abc", 'z') = false
str
- the String to check, may be nullsearchChar
- the character to findnull
string inputpublic static boolean contains(String str, String searchStr)
Checks if String contains a search String, handling null
.
This method uses String.indexOf(String)
.
A null
String will return false
.
ManStringUtil.contains(null, *) = false ManStringUtil.contains(*, null) = false ManStringUtil.contains("", "") = true ManStringUtil.contains("abc", "") = true ManStringUtil.contains("abc", "a") = true ManStringUtil.contains("abc", "z") = false
str
- the String to check, may be nullsearchStr
- the String to find, may be nullnull
string inputpublic static boolean containsIgnoreCase(String str, 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
.
ManStringUtil.contains(null, *) = false ManStringUtil.contains(*, null) = false ManStringUtil.contains("", "") = true ManStringUtil.contains("abc", "") = true ManStringUtil.contains("abc", "a") = true ManStringUtil.contains("abc", "z") = false ManStringUtil.contains("abc", "A") = true ManStringUtil.contains("abc", "Z") = false
str
- the String to check, may be nullsearchStr
- the String to find, may be nullnull
string inputpublic static int indexOfAny(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
.
ManStringUtil.indexOfAny(null, *) = -1 ManStringUtil.indexOfAny("", *) = -1 ManStringUtil.indexOfAny(*, null) = -1 ManStringUtil.indexOfAny(*, []) = -1 ManStringUtil.indexOfAny("zzabyycdxx",['z','a']) = 0 ManStringUtil.indexOfAny("zzabyycdxx",['b','y']) = 3 ManStringUtil.indexOfAny("aba", ['z']) = -1
str
- the String to check, may be nullsearchChars
- the chars to search for, may be nullpublic static int indexOfAny(String str, 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
.
ManStringUtil.indexOfAny(null, *) = -1 ManStringUtil.indexOfAny("", *) = -1 ManStringUtil.indexOfAny(*, null) = -1 ManStringUtil.indexOfAny(*, "") = -1 ManStringUtil.indexOfAny("zzabyycdxx", "za") = 0 ManStringUtil.indexOfAny("zzabyycdxx", "by") = 3 ManStringUtil.indexOfAny("aba","z") = -1
str
- the String to check, may be nullsearchChars
- the chars to search for, may be nullpublic static boolean containsAny(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
.
ManStringUtil.containsAny(null, *) = false ManStringUtil.containsAny("", *) = false ManStringUtil.containsAny(*, null) = false ManStringUtil.containsAny(*, []) = false ManStringUtil.containsAny("zzabyycdxx",['z','a']) = true ManStringUtil.containsAny("zzabyycdxx",['b','y']) = true ManStringUtil.containsAny("aba", ['z']) = false
str
- the String to check, may be nullsearchChars
- the chars to search for, may be nulltrue
if any of the chars are found,
false
if no match or null inputpublic static boolean containsAny(String str, String searchChars)
Checks if the String contains any character in the given set of characters.
A null
String will return false
. A null
search string will return
false
.
ManStringUtil.containsAny(null, *) = false ManStringUtil.containsAny("", *) = false ManStringUtil.containsAny(*, null) = false ManStringUtil.containsAny(*, "") = false ManStringUtil.containsAny("zzabyycdxx", "za") = true ManStringUtil.containsAny("zzabyycdxx", "by") = true ManStringUtil.containsAny("aba","z") = false
str
- the String to check, may be nullsearchChars
- the chars to search for, may be nulltrue
if any of the chars are found, false
if no match or null inputpublic 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.
A null
String will return -1
.
A null
or zero length search array will return -1
.
ManStringUtil.indexOfAnyBut(null, *) = -1 ManStringUtil.indexOfAnyBut("", *) = -1 ManStringUtil.indexOfAnyBut(*, null) = -1 ManStringUtil.indexOfAnyBut(*, []) = -1 ManStringUtil.indexOfAnyBut("zzabyycdxx",'za') = 3 ManStringUtil.indexOfAnyBut("zzabyycdxx", '') = 0 ManStringUtil.indexOfAnyBut("aba", 'ab') = -1
str
- the String to check, may be nullsearchChars
- the chars to search for, may be nullpublic 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.
A null
String will return -1
.
A null
search string will return -1
.
ManStringUtil.indexOfAnyBut(null, *) = -1 ManStringUtil.indexOfAnyBut("", *) = -1 ManStringUtil.indexOfAnyBut(*, null) = -1 ManStringUtil.indexOfAnyBut(*, "") = -1 ManStringUtil.indexOfAnyBut("zzabyycdxx", "za") = 3 ManStringUtil.indexOfAnyBut("zzabyycdxx", "") = 0 ManStringUtil.indexOfAnyBut("aba","ab") = -1
str
- the String to check, may be nullsearchChars
- the chars to search for, may be nullpublic static boolean containsOnly(String str, char[] valid)
Checks if the String contains only certain characters.
A null
String will return false
.
A null
valid character array will return false
.
An empty String ("") always returns true
.
ManStringUtil.containsOnly(null, *) = false ManStringUtil.containsOnly(*, null) = false ManStringUtil.containsOnly("", *) = true ManStringUtil.containsOnly("ab", '') = false ManStringUtil.containsOnly("abab", 'abc') = true ManStringUtil.containsOnly("ab1", 'abc') = false ManStringUtil.containsOnly("abz", 'abc') = false
str
- the String to check, may be nullvalid
- an array of valid chars, may be nullpublic static boolean containsOnly(String str, String validChars)
Checks if the String contains only certain characters.
A null
String will return false
.
A null
valid character String will return false
.
An empty String ("") always returns true
.
ManStringUtil.containsOnly(null, *) = false ManStringUtil.containsOnly(*, null) = false ManStringUtil.containsOnly("", *) = true ManStringUtil.containsOnly("ab", "") = false ManStringUtil.containsOnly("abab", "abc") = true ManStringUtil.containsOnly("ab1", "abc") = false ManStringUtil.containsOnly("abz", "abc") = false
str
- the String to check, may be nullvalidChars
- a String of valid chars, may be nullpublic static boolean containsNone(String str, char[] invalidChars)
Checks that the String does not contain certain characters.
A null
String will return true
.
A null
invalid character array will return true
.
An empty String ("") always returns true.
ManStringUtil.containsNone(null, *) = true ManStringUtil.containsNone(*, null) = true ManStringUtil.containsNone("", *) = true ManStringUtil.containsNone("ab", '') = true ManStringUtil.containsNone("abab", 'xyz') = true ManStringUtil.containsNone("ab1", 'xyz') = true ManStringUtil.containsNone("abz", 'xyz') = false
str
- the String to check, may be nullinvalidChars
- an array of invalid chars, may be nullpublic static boolean containsNone(String str, String invalidChars)
Checks that the String does not contain certain characters.
A null
String will return true
.
A null
invalid character array will return true
.
An empty String ("") always returns true.
ManStringUtil.containsNone(null, *) = true ManStringUtil.containsNone(*, null) = true ManStringUtil.containsNone("", *) = true ManStringUtil.containsNone("ab", "") = true ManStringUtil.containsNone("abab", "xyz") = true ManStringUtil.containsNone("ab1", "xyz") = true ManStringUtil.containsNone("abz", "xyz") = false
str
- the String to check, may be nullinvalidChars
- a String of invalid chars, may be nullpublic static int indexOfAny(String str, String[] searchStrs)
Find the first index of any of a set of potential substrings.
A null
String will return -1
.
A null
or zero length search array will return -1
.
A null
search array entry will be ignored, but a search
array containing "" will return 0
if str
is not
null. This method uses String.indexOf(String)
.
ManStringUtil.indexOfAny(null, *) = -1 ManStringUtil.indexOfAny(*, null) = -1 ManStringUtil.indexOfAny(*, []) = -1 ManStringUtil.indexOfAny("zzabyycdxx", ["ab","cd"]) = 2 ManStringUtil.indexOfAny("zzabyycdxx", ["cd","ab"]) = 2 ManStringUtil.indexOfAny("zzabyycdxx", ["mn","op"]) = -1 ManStringUtil.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1 ManStringUtil.indexOfAny("zzabyycdxx", [""]) = 0 ManStringUtil.indexOfAny("", [""]) = 0 ManStringUtil.indexOfAny("", ["a"]) = -1
str
- the String to check, may be nullsearchStrs
- the Strings to search for, may be nullpublic static int lastIndexOfAny(String str, String[] searchStrs)
Find the latest index of any of a set of potential substrings.
A null
String will return -1
.
A null
search array will return -1
.
A null
or zero length search array entry will be ignored,
but a search array containing "" will return the length of str
if str
is not null. This method uses String.indexOf(String)
ManStringUtil.lastIndexOfAny(null, *) = -1 ManStringUtil.lastIndexOfAny(*, null) = -1 ManStringUtil.lastIndexOfAny(*, []) = -1 ManStringUtil.lastIndexOfAny(*, [null]) = -1 ManStringUtil.lastIndexOfAny("zzabyycdxx", ["ab","cd"]) = 6 ManStringUtil.lastIndexOfAny("zzabyycdxx", ["cd","ab"]) = 6 ManStringUtil.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1 ManStringUtil.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1 ManStringUtil.lastIndexOfAny("zzabyycdxx", ["mn",""]) = 10
str
- the String to check, may be nullsearchStrs
- the Strings to search for, may be nullpublic static String substring(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
.
An empty ("") String will return "".
ManStringUtil.substring(null, *) = null ManStringUtil.substring("", *) = "" ManStringUtil.substring("abc", 0) = "abc" ManStringUtil.substring("abc", 2) = "c" ManStringUtil.substring("abc", 4) = "" ManStringUtil.substring("abc", -2) = "bc" ManStringUtil.substring("abc", -4) = "abc"
str
- the String to get the substring from, may be nullstart
- the position to start from, negative means
count back from the end of the String by this many charactersnull
if null String inputpublic static String substring(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. All position counting is
zero-based -- i.e., to start at the beginning of the string use
start = 0
. Negative start and end positions can be used to
specify offsets relative to the end of the String.
If start
is not strictly to the left of end
, ""
is returned.
ManStringUtil.substring(null, *, *) = null ManStringUtil.substring("", * , *) = ""; ManStringUtil.substring("abc", 0, 2) = "ab" ManStringUtil.substring("abc", 2, 0) = "" ManStringUtil.substring("abc", 2, 4) = "c" ManStringUtil.substring("abc", 4, 6) = "" ManStringUtil.substring("abc", 2, 2) = "" ManStringUtil.substring("abc", -2, -1) = "b" ManStringUtil.substring("abc", -4, 2) = "ab"
str
- the String to get the substring from, may be nullstart
- the position to start from, negative means
count back from the end of the String by this many charactersend
- the position to end at (exclusive), negative means
count back from the end of the String by this many charactersnull
if null String inputpublic static String left(String str, 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. An exception is thrown if len is negative.
ManStringUtil.left(null, *) = null ManStringUtil.left(*, -ve) = "" ManStringUtil.left("", *) = "" ManStringUtil.left("abc", 0) = "" ManStringUtil.left("abc", 2) = "ab" ManStringUtil.left("abc", 4) = "abc"
str
- the String to get the leftmost characters from, may be nulllen
- the length of the required String, must be zero or positivenull
if null String inputpublic static String right(String str, 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. An exception is thrown if len is negative.
ManStringUtil.right(null, *) = null ManStringUtil.right(*, -ve) = "" ManStringUtil.right("", *) = "" ManStringUtil.right("abc", 0) = "" ManStringUtil.right("abc", 2) = "bc" ManStringUtil.right("abc", 4) = "abc"
str
- the String to get the rightmost characters from, may be nulllen
- the length of the required String, must be zero or positivenull
if null String inputpublic static String mid(String str, int pos, 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. If the
String is null
, null
will be returned.
An exception is thrown if len is negative.
ManStringUtil.mid(null, *, *) = null ManStringUtil.mid(*, *, -ve) = "" ManStringUtil.mid("", 0, *) = "" ManStringUtil.mid("abc", 0, 2) = "ab" ManStringUtil.mid("abc", 0, 4) = "abc" ManStringUtil.mid("abc", 2, 4) = "c" ManStringUtil.mid("abc", 4, 2) = "" ManStringUtil.mid("abc", -2, 2) = "ab"
str
- the String to get the characters from, may be nullpos
- the position to start from, negative treated as zerolen
- the length of the required String, must be zero or positivenull
if null String inputpublic static String substringBefore(String str, String separator)
Gets the substring before the first occurrence of a separator. The separator is not returned.
A null
string input will return null
.
An empty ("") string input will return the empty string.
A null
separator will return the input string.
ManStringUtil.substringBefore(null, *) = null ManStringUtil.substringBefore("", *) = "" ManStringUtil.substringBefore("abc", "a") = "" ManStringUtil.substringBefore("abcba", "b") = "a" ManStringUtil.substringBefore("abc", "c") = "ab" ManStringUtil.substringBefore("abc", "d") = "abc" ManStringUtil.substringBefore("abc", "") = "" ManStringUtil.substringBefore("abc", null) = "abc"
str
- the String to get a substring from, may be nullseparator
- the String to search for, may be nullnull
if null String inputpublic static String substringAfter(String str, String separator)
Gets the substring after the first occurrence of a separator. The separator is not returned.
A null
string input will return null
.
An empty ("") string input will return the empty string.
A null
separator will return the empty string if the
input string is not null
.
ManStringUtil.substringAfter(null, *) = null ManStringUtil.substringAfter("", *) = "" ManStringUtil.substringAfter(*, null) = "" ManStringUtil.substringAfter("abc", "a") = "bc" ManStringUtil.substringAfter("abcba", "b") = "cba" ManStringUtil.substringAfter("abc", "c") = "" ManStringUtil.substringAfter("abc", "d") = "" ManStringUtil.substringAfter("abc", "") = "abc"
str
- the String to get a substring from, may be nullseparator
- the String to search for, may be nullnull
if null String inputpublic static String substringBeforeLast(String str, String separator)
Gets the substring before the last occurrence of a separator. The separator is not returned.
A null
string input will return null
.
An empty ("") string input will return the empty string.
An empty or null
separator will return the input string.
ManStringUtil.substringBeforeLast(null, *) = null ManStringUtil.substringBeforeLast("", *) = "" ManStringUtil.substringBeforeLast("abcba", "b") = "abc" ManStringUtil.substringBeforeLast("abc", "c") = "ab" ManStringUtil.substringBeforeLast("a", "a") = "" ManStringUtil.substringBeforeLast("a", "z") = "a" ManStringUtil.substringBeforeLast("a", null) = "a" ManStringUtil.substringBeforeLast("a", "") = "a"
str
- the String to get a substring from, may be nullseparator
- the String to search for, may be nullnull
if null String inputpublic static String substringAfterLast(String str, String separator)
Gets the substring after the last occurrence of a separator. The separator is not returned.
A null
string input will return null
.
An empty ("") string input will return the empty string.
An empty or null
separator will return the empty string if
the input string is not null
.
ManStringUtil.substringAfterLast(null, *) = null ManStringUtil.substringAfterLast("", *) = "" ManStringUtil.substringAfterLast(*, "") = "" ManStringUtil.substringAfterLast(*, null) = "" ManStringUtil.substringAfterLast("abc", "a") = "bc" ManStringUtil.substringAfterLast("abcba", "b") = "a" ManStringUtil.substringAfterLast("abc", "c") = "" ManStringUtil.substringAfterLast("a", "a") = "" ManStringUtil.substringAfterLast("a", "z") = ""
str
- the String to get a substring from, may be nullseparator
- the String to search for, may be nullnull
if null String inputpublic static String substringBetween(String str, String tag)
Gets the String that is nested in between two instances of the same String.
A null
input String returns null
.
A null
tag returns null
.
ManStringUtil.substringBetween(null, *) = null ManStringUtil.substringBetween("", "") = "" ManStringUtil.substringBetween("", "tag") = null ManStringUtil.substringBetween("tagabctag", null) = null ManStringUtil.substringBetween("tagabctag", "") = "" ManStringUtil.substringBetween("tagabctag", "tag") = "abc"
str
- the String containing the substring, may be nulltag
- the String before and after the substring, may be nullnull
if no matchpublic static String substringBetween(String str, String open, String close)
Gets the String that is nested in between two Strings. Only the first match is returned.
A null
input String returns null
.
A null
open/close returns null
(no match).
An empty ("") open and close returns an empty string.
ManStringUtil.substringBetween("wx[b]yz", "[", "]") = "b" ManStringUtil.substringBetween(null, *, *) = null ManStringUtil.substringBetween(*, null, *) = null ManStringUtil.substringBetween(*, *, null) = null ManStringUtil.substringBetween("", "", "") = "" ManStringUtil.substringBetween("", "", "]") = null ManStringUtil.substringBetween("", "[", "]") = null ManStringUtil.substringBetween("yabcz", "", "") = "" ManStringUtil.substringBetween("yabcz", "y", "z") = "abc" ManStringUtil.substringBetween("yabczyabcz", "y", "z") = "abc"
str
- the String containing the substring, may be nullopen
- the String before the substring, may be nullclose
- the String after the substring, may be nullnull
if no matchpublic static String[] substringsBetween(String str, String open, 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
.
A null
open/close returns null
(no match).
An empty ("") open/close returns null
(no match).
ManStringUtil.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"] ManStringUtil.substringsBetween(null, *, *) = null ManStringUtil.substringsBetween(*, null, *) = null ManStringUtil.substringsBetween(*, *, null) = null ManStringUtil.substringsBetween("", "[", "]") = []
str
- the String containing the substrings, null returns null, empty returns emptyopen
- the String identifying the start of the substring, empty returns nullclose
- the String identifying the end of the substring, empty returns nullnull
if no matchpublic static String getNestedString(String str, String tag)
substringBetween(String, String)
.
Method will be removed in Commons Lang 3.0.Gets the String that is nested in between two instances of the same String.
A null
input String returns null
.
A null
tag returns null
.
ManStringUtil.getNestedString(null, *) = null ManStringUtil.getNestedString("", "") = "" ManStringUtil.getNestedString("", "tag") = null ManStringUtil.getNestedString("tagabctag", null) = null ManStringUtil.getNestedString("tagabctag", "") = "" ManStringUtil.getNestedString("tagabctag", "tag") = "abc"
str
- the String containing nested-string, may be nulltag
- the String before and after nested-string, may be nullnull
if no matchpublic static String getNestedString(String str, String open, String close)
substringBetween(String, String, String)
.
Method will be removed in Commons Lang 3.0.Gets the String that is nested in between two Strings. Only the first match is returned.
A null
input String returns null
.
A null
open/close returns null
(no match).
An empty ("") open/close returns an empty string.
ManStringUtil.getNestedString(null, *, *) = null ManStringUtil.getNestedString("", "", "") = "" ManStringUtil.getNestedString("", "", "tag") = null ManStringUtil.getNestedString("", "tag", "tag") = null ManStringUtil.getNestedString("yabcz", null, null) = null ManStringUtil.getNestedString("yabcz", "", "") = "" ManStringUtil.getNestedString("yabcz", "y", "z") = "abc" ManStringUtil.getNestedString("yabczyabcz", "y", "z") = "abc"
str
- the String containing nested-string, may be nullopen
- the String before nested-string, may be nullclose
- the String after nested-string, may be nullnull
if no matchpublic static String[] split(String str)
Splits the provided text into an array, using whitespace as the
separator.
Whitespace is defined by Character.isWhitespace(char)
.
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
ManStringUtil.split(null) = null ManStringUtil.split("") = [] ManStringUtil.split("abc def") = ["abc", "def"] ManStringUtil.split("abc def") = ["abc", "def"] ManStringUtil.split(" abc ") = ["abc"]
str
- the String to parse, may be nullnull
if null String inputpublic static String[] split(String str, char separatorChar)
Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
ManStringUtil.split(null, *) = null ManStringUtil.split("", *) = [] ManStringUtil.split("a.b.c", '.') = ["a", "b", "c"] ManStringUtil.split("a..b.c", '.') = ["a", "b", "c"] ManStringUtil.split("a:b:c", '.') = ["a:b:c"] ManStringUtil.split("a b c", ' ') = ["a", "b", "c"]
str
- the String to parse, may be nullseparatorChar
- the character used as the delimiternull
if null String inputpublic static String[] split(String str, String separatorChars)
Splits the provided text into an array, separators specified. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
A null
separatorChars splits on whitespace.
ManStringUtil.split(null, *) = null ManStringUtil.split("", *) = [] ManStringUtil.split("abc def", null) = ["abc", "def"] ManStringUtil.split("abc def", " ") = ["abc", "def"] ManStringUtil.split("abc def", " ") = ["abc", "def"] ManStringUtil.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
str
- the String to parse, may be nullseparatorChars
- the characters used as the delimiters,
null
splits on whitespacenull
if null String inputpublic static String[] split(String str, String separatorChars, 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. Adjacent separators are treated as one separator.
A null
input String returns null
.
A null
separatorChars splits on whitespace.
If more than max
delimited substrings are found, the last
returned string includes all characters after the first max - 1
returned strings (including separator characters).
ManStringUtil.split(null, *, *) = null ManStringUtil.split("", *, *) = [] ManStringUtil.split("ab de fg", null, 0) = ["ab", "cd", "ef"] ManStringUtil.split("ab de fg", null, 0) = ["ab", "cd", "ef"] ManStringUtil.split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"] ManStringUtil.split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"]
str
- the String to parse, may be nullseparatorChars
- the characters used as the delimiters,
null
splits on whitespacemax
- the maximum number of elements to include in the
array. A zero or negative value implies no limitnull
if null String inputpublic static String[] splitByWholeSeparator(String str, String separator)
Splits the provided text into an array, separator string specified.
The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A null
input String returns null
.
A null
separator splits on whitespace.
ManStringUtil.splitByWholeSeparator(null, *) = null ManStringUtil.splitByWholeSeparator("", *) = [] ManStringUtil.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"] ManStringUtil.splitByWholeSeparator("ab de fg", null) = ["ab", "de", "fg"] ManStringUtil.splitByWholeSeparator("ab:cd:ef", ":") = ["ab", "cd", "ef"] ManStringUtil.splitByWholeSeparator("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
str
- the String to parse, may be nullseparator
- String containing the String to be used as a delimiter,
null
splits on whitespacenull
if null String was inputpublic static String[] splitByWholeSeparator(String str, String separator, int max)
Splits the provided text into an array, separator string specified.
Returns a maximum of max
substrings.
The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A null
input String returns null
.
A null
separator splits on whitespace.
ManStringUtil.splitByWholeSeparator(null, *, *) = null ManStringUtil.splitByWholeSeparator("", *, *) = [] ManStringUtil.splitByWholeSeparator("ab de fg", null, 0) = ["ab", "de", "fg"] ManStringUtil.splitByWholeSeparator("ab de fg", null, 0) = ["ab", "de", "fg"] ManStringUtil.splitByWholeSeparator("ab:cd:ef", ":", 2) = ["ab", "cd:ef"] ManStringUtil.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"] ManStringUtil.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
str
- the String to parse, may be nullseparator
- String containing the String to be used as a delimiter,
null
splits on whitespacemax
- the maximum number of elements to include in the returned
array. A zero or negative value implies no limit.null
if null String was inputpublic static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator)
Splits the provided text into an array, separator string specified.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
A null
separator splits on whitespace.
ManStringUtil.splitByWholeSeparatorPreserveAllTokens(null, *) = null ManStringUtil.splitByWholeSeparatorPreserveAllTokens("", *) = [] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab de fg", null) = ["ab", "de", "fg"] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab de fg", null) = ["ab", "", "", "de", "fg"] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab:cd:ef", ":") = ["ab", "cd", "ef"] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-") = ["ab", "cd", "ef"]
str
- the String to parse, may be nullseparator
- String containing the String to be used as a delimiter,
null
splits on whitespacenull
if null String was inputpublic static String[] splitByWholeSeparatorPreserveAllTokens(String str, String separator, int max)
Splits the provided text into an array, separator string specified.
Returns a maximum of max
substrings.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
A null
separator splits on whitespace.
ManStringUtil.splitByWholeSeparatorPreserveAllTokens(null, *, *) = null ManStringUtil.splitByWholeSeparatorPreserveAllTokens("", *, *) = [] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab de fg", null, 0) = ["ab", "de", "fg"] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab de fg", null, 0) = ["ab", "", "", "de", "fg"] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab:cd:ef", ":", 2) = ["ab", "cd:ef"] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"] ManStringUtil.splitByWholeSeparatorPreserveAllTokens("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
str
- the String to parse, may be nullseparator
- String containing the String to be used as a delimiter,
null
splits on whitespacemax
- the maximum number of elements to include in the returned
array. A zero or negative value implies no limit.null
if null String was inputpublic static String[] splitPreserveAllTokens(String str)
Splits the provided text into an array, using whitespace as the
separator, preserving all tokens, including empty tokens created by
adjacent separators. This is an alternative to using StringTokenizer.
Whitespace is defined by Character.isWhitespace(char)
.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
ManStringUtil.splitPreserveAllTokens(null) = null ManStringUtil.splitPreserveAllTokens("") = [] ManStringUtil.splitPreserveAllTokens("abc def") = ["abc", "def"] ManStringUtil.splitPreserveAllTokens("abc def") = ["abc", "", "def"] ManStringUtil.splitPreserveAllTokens(" abc ") = ["", "abc", ""]
str
- the String to parse, may be null
null
if null String inputpublic static String[] splitPreserveAllTokens(String str, char separatorChar)
Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
ManStringUtil.splitPreserveAllTokens(null, *) = null ManStringUtil.splitPreserveAllTokens("", *) = [] ManStringUtil.splitPreserveAllTokens("a.b.c", '.') = ["a", "b", "c"] ManStringUtil.splitPreserveAllTokens("a..b.c", '.') = ["a", "", "b", "c"] ManStringUtil.splitPreserveAllTokens("a:b:c", '.') = ["a:b:c"] ManStringUtil.splitPreserveAllTokens("a\tb\nc", null) = ["a", "b", "c"] ManStringUtil.splitPreserveAllTokens("a b c", ' ') = ["a", "b", "c"] ManStringUtil.splitPreserveAllTokens("a b c ", ' ') = ["a", "b", "c", ""] ManStringUtil.splitPreserveAllTokens("a b c ", ' ') = ["a", "b", "c", "", ""] ManStringUtil.splitPreserveAllTokens(" a b c", ' ') = ["", a", "b", "c"] ManStringUtil.splitPreserveAllTokens(" a b c", ' ') = ["", "", a", "b", "c"] ManStringUtil.splitPreserveAllTokens(" a b c ", ' ') = ["", a", "b", "c", ""]
str
- the String to parse, may be null
separatorChar
- the character used as the delimiter,
null
splits on whitespacenull
if null String inputpublic static String[] splitPreserveAllTokens(String str, String separatorChars)
Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. For more control over the split use the StrTokenizer class.
A null
input String returns null
.
A null
separatorChars splits on whitespace.
ManStringUtil.splitPreserveAllTokens(null, *) = null ManStringUtil.splitPreserveAllTokens("", *) = [] ManStringUtil.splitPreserveAllTokens("abc def", null) = ["abc", "def"] ManStringUtil.splitPreserveAllTokens("abc def", " ") = ["abc", "def"] ManStringUtil.splitPreserveAllTokens("abc def", " ") = ["abc", "", def"] ManStringUtil.splitPreserveAllTokens("ab:cd:ef", ":") = ["ab", "cd", "ef"] ManStringUtil.splitPreserveAllTokens("ab:cd:ef:", ":") = ["ab", "cd", "ef", ""] ManStringUtil.splitPreserveAllTokens("ab:cd:ef::", ":") = ["ab", "cd", "ef", "", ""] ManStringUtil.splitPreserveAllTokens("ab::cd:ef", ":") = ["ab", "", cd", "ef"] ManStringUtil.splitPreserveAllTokens(":cd:ef", ":") = ["", cd", "ef"] ManStringUtil.splitPreserveAllTokens("::cd:ef", ":") = ["", "", cd", "ef"] ManStringUtil.splitPreserveAllTokens(":cd:ef:", ":") = ["", cd", "ef", ""]
str
- the String to parse, may be null
separatorChars
- the characters used as the delimiters,
null
splits on whitespacenull
if null String inputpublic static String[] splitPreserveAllTokens(String str, 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.
The separator is not included in the returned String array. Adjacent separators are treated as separators for empty tokens. Adjacent separators are treated as one separator.
A null
input String returns null
.
A null
separatorChars splits on whitespace.
If more than max
delimited substrings are found, the last
returned string includes all characters after the first max - 1
returned strings (including separator characters).
ManStringUtil.splitPreserveAllTokens(null, *, *) = null ManStringUtil.splitPreserveAllTokens("", *, *) = [] ManStringUtil.splitPreserveAllTokens("ab de fg", null, 0) = ["ab", "cd", "ef"] ManStringUtil.splitPreserveAllTokens("ab de fg", null, 0) = ["ab", "cd", "ef"] ManStringUtil.splitPreserveAllTokens("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"] ManStringUtil.splitPreserveAllTokens("ab:cd:ef", ":", 2) = ["ab", "cd:ef"] ManStringUtil.splitPreserveAllTokens("ab de fg", null, 2) = ["ab", " de fg"] ManStringUtil.splitPreserveAllTokens("ab de fg", null, 3) = ["ab", "", " de fg"] ManStringUtil.splitPreserveAllTokens("ab de fg", null, 4) = ["ab", "", "", "de fg"]
str
- the String to parse, may be null
separatorChars
- the characters used as the delimiters,
null
splits on whitespacemax
- the maximum number of elements to include in the
array. A zero or negative value implies no limitnull
if null String inputpublic static String[] splitByCharacterType(String str)
Splits a String by Character type as returned by
java.lang.Character.getType(char)
. Groups of contiguous
characters of the same type are returned as complete tokens.
ManStringUtil.splitByCharacterType(null) = null ManStringUtil.splitByCharacterType("") = [] ManStringUtil.splitByCharacterType("ab de fg") = ["ab", " ", "de", " ", "fg"] ManStringUtil.splitByCharacterType("ab de fg") = ["ab", " ", "de", " ", "fg"] ManStringUtil.splitByCharacterType("ab:cd:ef") = ["ab", ":", "cd", ":", "ef"] ManStringUtil.splitByCharacterType("number5") = ["number", "5"] ManStringUtil.splitByCharacterType("fooBar") = ["foo", "B", "ar"] ManStringUtil.splitByCharacterType("foo200Bar") = ["foo", "200", "B", "ar"] ManStringUtil.splitByCharacterType("ASFRules") = ["ASFR", "ules"]
str
- the String to split, may be null
null
if null String inputpublic static String[] splitByCharacterTypeCamelCase(String str)
Splits a String by Character type as returned by
java.lang.Character.getType(char)
. Groups of contiguous
characters of the same type are returned as complete tokens, with the
following exception: the character of type
Character.UPPERCASE_LETTER
, if any, immediately
preceding a token of type Character.LOWERCASE_LETTER
will belong to the following token rather than to the preceding, if any,
Character.UPPERCASE_LETTER
token.
ManStringUtil.splitByCharacterTypeCamelCase(null) = null ManStringUtil.splitByCharacterTypeCamelCase("") = [] ManStringUtil.splitByCharacterTypeCamelCase("ab de fg") = ["ab", " ", "de", " ", "fg"] ManStringUtil.splitByCharacterTypeCamelCase("ab de fg") = ["ab", " ", "de", " ", "fg"] ManStringUtil.splitByCharacterTypeCamelCase("ab:cd:ef") = ["ab", ":", "cd", ":", "ef"] ManStringUtil.splitByCharacterTypeCamelCase("number5") = ["number", "5"] ManStringUtil.splitByCharacterTypeCamelCase("fooBar") = ["foo", "Bar"] ManStringUtil.splitByCharacterTypeCamelCase("foo200Bar") = ["foo", "200", "Bar"] ManStringUtil.splitByCharacterTypeCamelCase("ASFRules") = ["ASF", "Rules"]
str
- the String to split, may be null
null
if null String inputpublic static String concatenate(Object[] array)
join(Object[])
instead.
Method will be removed in Commons Lang 3.0.Concatenates elements of an array into a single String. Null objects or empty strings within the array are represented by empty strings.
ManStringUtil.concatenate(null) = null ManStringUtil.concatenate([]) = "" ManStringUtil.concatenate([null]) = "" ManStringUtil.concatenate(["a", "b", "c"]) = "abc" ManStringUtil.concatenate([null, "", "a"]) = "a"
array
- the array of values to concatenate, may be nullnull
if null array inputpublic static String join(Object[] array)
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. Null objects or empty strings within the array are represented by empty strings.
ManStringUtil.join(null) = null ManStringUtil.join([]) = "" ManStringUtil.join([null]) = "" ManStringUtil.join(["a", "b", "c"]) = "abc" ManStringUtil.join([null, "", "a"]) = "a"
array
- the array of values to join together, may be nullnull
if null array inputpublic static String join(Object[] array, 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. Null objects or empty strings within the array are represented by empty strings.
ManStringUtil.join(null, *) = null ManStringUtil.join([], *) = "" ManStringUtil.join([null], *) = "" ManStringUtil.join(["a", "b", "c"], ';') = "a;b;c" ManStringUtil.join(["a", "b", "c"], null) = "abc" ManStringUtil.join([null, "", "a"], ';') = ";;a"
array
- the array of values to join together, may be nullseparator
- the separator character to usenull
if null array inputpublic static String join(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.
No delimiter is added before or after the list. Null objects or empty strings within the array are represented by empty strings.
ManStringUtil.join(null, *) = null ManStringUtil.join([], *) = "" ManStringUtil.join([null], *) = "" ManStringUtil.join(["a", "b", "c"], ';') = "a;b;c" ManStringUtil.join(["a", "b", "c"], null) = "abc" ManStringUtil.join([null, "", "a"], ';') = ";;a"
array
- the array of values to join together, may be nullseparator
- the separator character to usestartIndex
- the first index to start joining from. It is
an error to pass in an end index past the end of the arrayendIndex
- the index to stop joining from (exclusive). It is
an error to pass in an end index past the end of the arraynull
if null array inputpublic static String join(Object[] array, 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.
A null
separator is the same as an empty String ("").
Null objects or empty strings within the array are represented by
empty strings.
ManStringUtil.join(null, *) = null ManStringUtil.join([], *) = "" ManStringUtil.join([null], *) = "" ManStringUtil.join(["a", "b", "c"], "--") = "a--b--c" ManStringUtil.join(["a", "b", "c"], null) = "abc" ManStringUtil.join(["a", "b", "c"], "") = "abc" ManStringUtil.join([null, "", "a"], ',') = ",,a"
array
- the array of values to join together, may be nullseparator
- the separator character to use, null treated as ""null
if null array inputpublic static String join(Object[] array, String separator, int startIndex, 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.
A null
separator is the same as an empty String ("").
Null objects or empty strings within the array are represented by
empty strings.
ManStringUtil.join(null, *) = null ManStringUtil.join([], *) = "" ManStringUtil.join([null], *) = "" ManStringUtil.join(["a", "b", "c"], "--") = "a--b--c" ManStringUtil.join(["a", "b", "c"], null) = "abc" ManStringUtil.join(["a", "b", "c"], "") = "abc" ManStringUtil.join([null, "", "a"], ',') = ",,a"
array
- the array of values to join together, may be nullseparator
- the separator character to use, null treated as ""startIndex
- the first index to start joining from. It is
an error to pass in an end index past the end of the arrayendIndex
- the index to stop joining from (exclusive). It is
an error to pass in an end index past the end of the arraynull
if null array inputpublic static String join(Iterator iterator, 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. Null objects or empty strings within the iteration are represented by empty strings.
See the examples here: join(Object[], char)
.
iterator
- the Iterator
of values to join together, may be nullseparator
- the separator character to usenull
if null iterator inputpublic static String join(Iterator iterator, 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.
A null
separator is the same as an empty String ("").
See the examples here: join(Object[], String)
.
iterator
- the Iterator
of values to join together, may be nullseparator
- the separator character to use, null treated as ""null
if null iterator inputpublic static String join(Collection collection, char separator)
Joins the elements of the provided Collection
into
a single String containing the provided elements.
No delimiter is added before or after the list. Null objects or empty strings within the iteration are represented by empty strings.
See the examples here: join(Object[], char)
.
collection
- the Collection
of values to join together, may be nullseparator
- the separator character to usenull
if null iterator inputpublic static String join(Collection collection, String separator)
Joins the elements of the provided Collection
into
a single String containing the provided elements.
No delimiter is added before or after the list.
A null
separator is the same as an empty String ("").
See the examples here: join(Object[], String)
.
collection
- the Collection
of values to join together, may be nullseparator
- the separator character to use, null treated as ""null
if null iterator inputpublic static String deleteWhitespace(String str)
Deletes all whitespaces from a String as defined by
Character.isWhitespace(char)
.
ManStringUtil.deleteWhitespace(null) = null ManStringUtil.deleteWhitespace("") = "" ManStringUtil.deleteWhitespace("abc") = "abc" ManStringUtil.deleteWhitespace(" ab c ") = "abc"
str
- the String to delete whitespace from, may be nullnull
if null String inputpublic static String removeStart(String str, String remove)
Removes a substring only if it is at the begining of a source string, otherwise returns the source string.
A null
source string will return null
.
An empty ("") source string will return the empty string.
A null
search string will return the source string.
ManStringUtil.removeStart(null, *) = null ManStringUtil.removeStart("", *) = "" ManStringUtil.removeStart(*, null) = * ManStringUtil.removeStart("www.domain.com", "www.") = "domain.com" ManStringUtil.removeStart("domain.com", "www.") = "domain.com" ManStringUtil.removeStart("www.domain.com", "domain") = "www.domain.com" ManStringUtil.removeStart("abc", "") = "abc"
str
- the source String to search, may be nullremove
- the String to search for and remove, may be nullnull
if null String inputpublic static String removeStartIgnoreCase(String str, String remove)
Case insensitive removal of a substring if it is at the begining of a source string, otherwise returns the source string.
A null
source string will return null
.
An empty ("") source string will return the empty string.
A null
search string will return the source string.
ManStringUtil.removeStartIgnoreCase(null, *) = null ManStringUtil.removeStartIgnoreCase("", *) = "" ManStringUtil.removeStartIgnoreCase(*, null) = * ManStringUtil.removeStartIgnoreCase("www.domain.com", "www.") = "domain.com" ManStringUtil.removeStartIgnoreCase("www.domain.com", "WWW.") = "domain.com" ManStringUtil.removeStartIgnoreCase("domain.com", "www.") = "domain.com" ManStringUtil.removeStartIgnoreCase("www.domain.com", "domain") = "www.domain.com" ManStringUtil.removeStartIgnoreCase("abc", "") = "abc"
str
- the source String to search, may be nullremove
- the String to search for (case insensitive) and remove, may be nullnull
if null String inputpublic static String removeEnd(String str, 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
.
An empty ("") source string will return the empty string.
A null
search string will return the source string.
ManStringUtil.removeEnd(null, *) = null ManStringUtil.removeEnd("", *) = "" ManStringUtil.removeEnd(*, null) = * ManStringUtil.removeEnd("www.domain.com", ".com.") = "www.domain.com" ManStringUtil.removeEnd("www.domain.com", ".com") = "www.domain" ManStringUtil.removeEnd("www.domain.com", "domain") = "www.domain.com" ManStringUtil.removeEnd("abc", "") = "abc"
str
- the source String to search, may be nullremove
- the String to search for and remove, may be nullnull
if null String inputpublic static String removeEndIgnoreCase(String str, 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
.
An empty ("") source string will return the empty string.
A null
search string will return the source string.
ManStringUtil.removeEnd(null, *) = null ManStringUtil.removeEnd("", *) = "" ManStringUtil.removeEnd(*, null) = * ManStringUtil.removeEnd("www.domain.com", ".com.") = "www.domain.com." ManStringUtil.removeEnd("www.domain.com", ".com") = "www.domain" ManStringUtil.removeEnd("www.domain.com", "domain") = "www.domain.com" ManStringUtil.removeEnd("abc", "") = "abc"
str
- the source String to search, may be nullremove
- the String to search for (case insensitive) and remove, may be nullnull
if null String inputpublic static String remove(String str, String remove)
Removes all occurrences of a substring from within the source string.
A null
source string will return null
.
An empty ("") source string will return the empty string.
A null
remove string will return the source string.
An empty ("") remove string will return the source string.
ManStringUtil.remove(null, *) = null ManStringUtil.remove("", *) = "" ManStringUtil.remove(*, null) = * ManStringUtil.remove(*, "") = * ManStringUtil.remove("queued", "ue") = "qd" ManStringUtil.remove("queued", "zz") = "queued"
str
- the source String to search, may be nullremove
- the String to search for and remove, may be nullnull
if null String inputpublic static String remove(String str, char remove)
Removes all occurrences of a character from within the source string.
A null
source string will return null
.
An empty ("") source string will return the empty string.
ManStringUtil.remove(null, *) = null ManStringUtil.remove("", *) = "" ManStringUtil.remove("queued", 'u') = "qeed" ManStringUtil.remove("queued", 'z') = "queued"
str
- the source String to search, may be nullremove
- the char to search for and remove, may be nullnull
if null String inputpublic static String replaceOnce(String text, String searchString, String replacement)
Replaces a String with another String inside a larger String, once.
A null
reference passed to this method is a no-op.
ManStringUtil.replaceOnce(null, *, *) = null ManStringUtil.replaceOnce("", *, *) = "" ManStringUtil.replaceOnce("any", null, *) = "any" ManStringUtil.replaceOnce("any", *, null) = "any" ManStringUtil.replaceOnce("any", "", *) = "any" ManStringUtil.replaceOnce("aba", "a", null) = "aba" ManStringUtil.replaceOnce("aba", "a", "") = "ba" ManStringUtil.replaceOnce("aba", "a", "z") = "zba"
text
- text to search and replace in, may be nullsearchString
- the String to search for, may be nullreplacement
- the String to replace with, may be nullnull
if null String inputreplace(String text, String searchString, String replacement, int max)
public static String replace(String text, String searchString, String replacement)
Replaces all occurrences of a String within another String.
A null
reference passed to this method is a no-op.
ManStringUtil.replace(null, *, *) = null ManStringUtil.replace("", *, *) = "" ManStringUtil.replace("any", null, *) = "any" ManStringUtil.replace("any", *, null) = "any" ManStringUtil.replace("any", "", *) = "any" ManStringUtil.replace("aba", "a", null) = "aba" ManStringUtil.replace("aba", "a", "") = "b" ManStringUtil.replace("aba", "a", "z") = "zbz"
text
- text to search and replace in, may be nullsearchString
- the String to search for, may be nullreplacement
- the String to replace it with, may be nullnull
if null String inputreplace(String text, String searchString, String replacement, int max)
public static String replace(String text, String searchString, 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.
ManStringUtil.replace(null, *, *, *) = null ManStringUtil.replace("", *, *, *) = "" ManStringUtil.replace("any", null, *, *) = "any" ManStringUtil.replace("any", *, null, *) = "any" ManStringUtil.replace("any", "", *, *) = "any" ManStringUtil.replace("any", *, *, 0) = "any" ManStringUtil.replace("abaa", "a", null, -1) = "abaa" ManStringUtil.replace("abaa", "a", "", -1) = "b" ManStringUtil.replace("abaa", "a", "z", 0) = "abaa" ManStringUtil.replace("abaa", "a", "z", 1) = "zbaa" ManStringUtil.replace("abaa", "a", "z", 2) = "zbza" ManStringUtil.replace("abaa", "a", "z", -1) = "zbzz"
text
- text to search and replace in, may be nullsearchString
- the String to search for, may be nullreplacement
- the String to replace it with, may be nullmax
- maximum number of values to replace, or -1
if no maximumnull
if null String inputpublic static String replaceEach(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.
A null
reference passed to this method is a no-op, or if
any "search string" or "string to replace" is null, that replace will be
ignored. This will not repeat. For repeating replaces, call the
overloaded method.
ManStringUtil.replaceEach(null, *, *) = null ManStringUtil.replaceEach("", *, *) = "" ManStringUtil.replaceEach("aba", null, null) = "aba" ManStringUtil.replaceEach("aba", new String[0], null) = "aba" ManStringUtil.replaceEach("aba", null, new String[0]) = "aba" ManStringUtil.replaceEach("aba", new String[]{"a"}, null) = "aba" ManStringUtil.replaceEach("aba", new String[]{"a"}, new String[]{""}) = "b" ManStringUtil.replaceEach("aba", new String[]{null}, new String[]{"a"}) = "aba" ManStringUtil.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}) = "wcte" (example of how it does not repeat) ManStringUtil.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}) = "dcte"
text
- text to search and replace in, no-op if nullsearchList
- the Strings to search for, no-op if nullreplacementList
- the Strings to replace them with, no-op if nullnull
if
null String inputIndexOutOfBoundsException
- if the lengths of the arrays are not the same (null is ok,
and/or size 0)public static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.
A null
reference passed to this method is a no-op, or if
any "search string" or "string to replace" is null, that replace will be
ignored. This will not repeat. For repeating replaces, call the
overloaded method.
ManStringUtil.replaceEach(null, *, *, *) = null ManStringUtil.replaceEach("", *, *, *) = "" ManStringUtil.replaceEach("aba", null, null, *) = "aba" ManStringUtil.replaceEach("aba", new String[0], null, *) = "aba" ManStringUtil.replaceEach("aba", null, new String[0], *) = "aba" ManStringUtil.replaceEach("aba", new String[]{"a"}, null, *) = "aba" ManStringUtil.replaceEach("aba", new String[]{"a"}, new String[]{""}, *) = "b" ManStringUtil.replaceEach("aba", new String[]{null}, new String[]{"a"}, *) = "aba" ManStringUtil.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}, *) = "wcte" (example of how it repeats) ManStringUtil.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, false) = "dcte" ManStringUtil.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}, true) = "tcte" ManStringUtil.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, true) = IllegalArgumentException ManStringUtil.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "ab"}, false) = "dcabe"
text
- text to search and replace in, no-op if nullsearchList
- the Strings to search for, no-op if nullreplacementList
- the Strings to replace them with, no-op if nullnull
if
null String inputIllegalArgumentException
- if the search is repeating and there is an endless loop due
to outputs of one being inputs to anotherIndexOutOfBoundsException
- if the lengths of the arrays are not the same (null is ok,
and/or size 0)public static String replaceChars(String str, char searchChar, char replaceChar)
Replaces all occurrences of a character in a String with another.
This is a null-safe version of String.replace(char, char)
.
A null
string input returns null
.
An empty ("") string input returns an empty string.
ManStringUtil.replaceChars(null, *, *) = null ManStringUtil.replaceChars("", *, *) = "" ManStringUtil.replaceChars("abcba", 'b', 'y') = "aycya" ManStringUtil.replaceChars("abcba", 'z', 'y') = "abcba"
str
- String to replace characters in, may be nullsearchChar
- the character to search for, may be nullreplaceChar
- the character to replace, may be nullnull
if null string inputpublic static String replaceChars(String str, String searchChars, String replaceChars)
Replaces multiple characters in a String in one go. This method can also be used to delete characters.
For example:
replaceChars("hello", "ho", "jy") = jelly
.
A null
string input returns null
.
An empty ("") string input returns an empty string.
A null or empty set of search characters returns the input string.
The length of the search characters should normally equal the length of the replace characters. If the search characters is longer, then the extra search characters are deleted. If the search characters is shorter, then the extra replace characters are ignored.
ManStringUtil.replaceChars(null, *, *) = null ManStringUtil.replaceChars("", *, *) = "" ManStringUtil.replaceChars("abc", null, *) = "abc" ManStringUtil.replaceChars("abc", "", *) = "abc" ManStringUtil.replaceChars("abc", "b", null) = "ac" ManStringUtil.replaceChars("abc", "b", "") = "ac" ManStringUtil.replaceChars("abcba", "bc", "yz") = "ayzya" ManStringUtil.replaceChars("abcba", "bc", "y") = "ayya" ManStringUtil.replaceChars("abcba", "bc", "yzx") = "ayzya"
str
- String to replace characters in, may be nullsearchChars
- a set of characters to search for, may be nullreplaceChars
- a set of characters to replace, may be nullnull
if null string inputpublic static String overlayString(String text, String overlay, int start, int end)
overlay(String, String, int, int)
instead.
Method will be removed in Commons Lang 3.0.Overlays part of a String with another String.
ManStringUtil.overlayString(null, *, *, *) = NullPointerException ManStringUtil.overlayString(*, null, *, *) = NullPointerException ManStringUtil.overlayString("", "abc", 0, 0) = "abc" ManStringUtil.overlayString("abcdef", null, 2, 4) = "abef" ManStringUtil.overlayString("abcdef", "", 2, 4) = "abef" ManStringUtil.overlayString("abcdef", "zzzz", 2, 4) = "abzzzzef" ManStringUtil.overlayString("abcdef", "zzzz", 4, 2) = "abcdzzzzcdef" ManStringUtil.overlayString("abcdef", "zzzz", -1, 4) = IndexOutOfBoundsException ManStringUtil.overlayString("abcdef", "zzzz", 2, 8) = IndexOutOfBoundsException
text
- the String to do overlaying in, may be nulloverlay
- the String to overlay, may be nullstart
- the position to start overlaying at, must be validend
- the position to stop overlaying before, must be validnull
if null String inputNullPointerException
- if text or overlay is nullIndexOutOfBoundsException
- if either position is invalidpublic static String overlay(String str, String overlay, int start, int end)
Overlays part of a String with another String.
A null
string input returns null
.
A negative index is treated as zero.
An index greater than the string length is treated as the string length.
The start index is always the smaller of the two indices.
ManStringUtil.overlay(null, *, *, *) = null ManStringUtil.overlay("", "abc", 0, 0) = "abc" ManStringUtil.overlay("abcdef", null, 2, 4) = "abef" ManStringUtil.overlay("abcdef", "", 2, 4) = "abef" ManStringUtil.overlay("abcdef", "", 4, 2) = "abef" ManStringUtil.overlay("abcdef", "zzzz", 2, 4) = "abzzzzef" ManStringUtil.overlay("abcdef", "zzzz", 4, 2) = "abzzzzef" ManStringUtil.overlay("abcdef", "zzzz", -1, 4) = "zzzzef" ManStringUtil.overlay("abcdef", "zzzz", 2, 8) = "abzzzz" ManStringUtil.overlay("abcdef", "zzzz", -2, -3) = "zzzzabcdef" ManStringUtil.overlay("abcdef", "zzzz", 8, 10) = "abcdefzzzz"
str
- the String to do overlaying in, may be nulloverlay
- the String to overlay, may be nullstart
- the position to start overlaying atend
- the position to stop overlaying beforenull
if null String inputpublic static boolean isCrLf(String str)
public static String chomp(String str)
Removes one newline from end of a String if it's there,
otherwise leave it alone. A newline is "\n
",
"\r
", or "\r\n
".
NOTE: This method changed in 2.0. It now more closely matches Perl chomp.
ManStringUtil.chomp(null) = null ManStringUtil.chomp("") = "" ManStringUtil.chomp("abc \r") = "abc " ManStringUtil.chomp("abc\n") = "abc" ManStringUtil.chomp("abc\r\n") = "abc" ManStringUtil.chomp("abc\r\n\r\n") = "abc\r\n" ManStringUtil.chomp("abc\n\r") = "abc\n" ManStringUtil.chomp("abc\n\rabc") = "abc\n\rabc" ManStringUtil.chomp("\r") = "" ManStringUtil.chomp("\n") = "" ManStringUtil.chomp("\r\n") = ""
str
- the String to chomp a newline from, may be nullnull
if null String inputpublic static String chomp(String str, 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.
It now more closely matches Perl chomp.
For the previous behavior, use substringBeforeLast(String, String)
.
This method uses String.endsWith(String)
.
ManStringUtil.chomp(null, *) = null ManStringUtil.chomp("", *) = "" ManStringUtil.chomp("foobar", "bar") = "foo" ManStringUtil.chomp("foobar", "baz") = "foobar" ManStringUtil.chomp("foo", "foo") = "" ManStringUtil.chomp("foo ", "foo") = "foo " ManStringUtil.chomp(" foo", "foo") = " " ManStringUtil.chomp("foo", "foooo") = "foo" ManStringUtil.chomp("foo", "") = "foo" ManStringUtil.chomp("foo", null) = "foo"
str
- the String to chomp from, may be nullseparator
- separator String, may be nullnull
if null String inputpublic static String chompLast(String str)
chomp(String)
instead.
Method will be removed in Commons Lang 3.0.Remove any "\n" if and only if it is at the end of the supplied String.
str
- the String to chomp from, must not be nullNullPointerException
- if str is null
public static String chompLast(String str, String sep)
chomp(String, String)
instead.
Method will be removed in Commons Lang 3.0.Remove a value if and only if the String ends with that value.
str
- the String to chomp from, must not be nullsep
- the String to chomp, must not be nullNullPointerException
- if str or sep is null
public static String getChomp(String str, String sep)
substringAfterLast(String, String)
instead
(although this doesn't include the separator)
Method will be removed in Commons Lang 3.0.Remove everything and return the last value of a supplied String, and everything after it from a String.
str
- the String to chomp from, must not be nullsep
- the String to chomp, must not be nullNullPointerException
- if str or sep is null
public static String prechomp(String str, String sep)
substringAfter(String, String)
instead.
Method will be removed in Commons Lang 3.0.Remove the first value of a supplied String, and everything before it from a String.
str
- the String to chomp from, must not be nullsep
- the String to chomp, must not be nullNullPointerException
- if str or sep is null
public static String getPrechomp(String str, String sep)
substringBefore(String, String)
instead
(although this doesn't include the separator).
Method will be removed in Commons Lang 3.0.Remove and return everything before the first value of a supplied String from another String.
str
- the String to chomp from, must not be nullsep
- the String to chomp, must not be nullNullPointerException
- if str or sep is null
public static String chop(String str)
Remove the last character from a String.
If the String ends in \r\n
, then remove both
of them.
ManStringUtil.chop(null) = null ManStringUtil.chop("") = "" ManStringUtil.chop("abc \r") = "abc " ManStringUtil.chop("abc\n") = "abc" ManStringUtil.chop("abc\r\n") = "abc" ManStringUtil.chop("abc") = "ab" ManStringUtil.chop("abc\nabc") = "abc\nab" ManStringUtil.chop("a") = "" ManStringUtil.chop("\r") = "" ManStringUtil.chop("\n") = "" ManStringUtil.chop("\r\n") = ""
str
- the String to chop last character from, may be nullnull
if null String inputpublic static String chopNewline(String str)
chomp(String)
instead.
Method will be removed in Commons Lang 3.0.Removes \n
from end of a String if it's there.
If a \r
precedes it, then remove that too.
str
- the String to chop a newline from, must not be nullNullPointerException
- if str is null
public static String repeat(String str, int repeat)
Repeat a String repeat
times to form a
new String.
ManStringUtil.repeat(null, 2) = null ManStringUtil.repeat("", 0) = "" ManStringUtil.repeat("", 2) = "" ManStringUtil.repeat("a", 3) = "aaa" ManStringUtil.repeat("ab", 2) = "abab" ManStringUtil.repeat("a", -2) = ""
str
- the String to repeat, may be nullrepeat
- number of times to repeat str, negative treated as zeronull
if null String inputpublic static String rightPad(String str, int size)
Right pad a String with spaces (' ').
The String is padded to the size of size
.
ManStringUtil.rightPad(null, *) = null ManStringUtil.rightPad("", 3) = " " ManStringUtil.rightPad("bat", 3) = "bat" ManStringUtil.rightPad("bat", 5) = "bat " ManStringUtil.rightPad("bat", 1) = "bat" ManStringUtil.rightPad("bat", -1) = "bat"
str
- the String to pad out, may be nullsize
- the size to pad tonull
if null String inputpublic static String rightPad(String str, int size, char padChar)
Right pad a String with a specified character.
The String is padded to the size of size
.
ManStringUtil.rightPad(null, *, *) = null ManStringUtil.rightPad("", 3, 'z') = "zzz" ManStringUtil.rightPad("bat", 3, 'z') = "bat" ManStringUtil.rightPad("bat", 5, 'z') = "batzz" ManStringUtil.rightPad("bat", 1, 'z') = "bat" ManStringUtil.rightPad("bat", -1, 'z') = "bat"
str
- the String to pad out, may be nullsize
- the size to pad topadChar
- the character to pad withnull
if null String inputpublic static String rightPad(String str, int size, String padStr)
Right pad a String with a specified String.
The String is padded to the size of size
.
ManStringUtil.rightPad(null, *, *) = null ManStringUtil.rightPad("", 3, "z") = "zzz" ManStringUtil.rightPad("bat", 3, "yz") = "bat" ManStringUtil.rightPad("bat", 5, "yz") = "batyz" ManStringUtil.rightPad("bat", 8, "yz") = "batyzyzy" ManStringUtil.rightPad("bat", 1, "yz") = "bat" ManStringUtil.rightPad("bat", -1, "yz") = "bat" ManStringUtil.rightPad("bat", 5, null) = "bat " ManStringUtil.rightPad("bat", 5, "") = "bat "
str
- the String to pad out, may be nullsize
- the size to pad topadStr
- the String to pad with, null or empty treated as single spacenull
if null String inputpublic static String leftPad(String str, int size)
Left pad a String with spaces (' ').
The String is padded to the size of size
.
ManStringUtil.leftPad(null, *) = null ManStringUtil.leftPad("", 3) = " " ManStringUtil.leftPad("bat", 3) = "bat" ManStringUtil.leftPad("bat", 5) = " bat" ManStringUtil.leftPad("bat", 1) = "bat" ManStringUtil.leftPad("bat", -1) = "bat"
str
- the String to pad out, may be nullsize
- the size to pad tonull
if null String inputpublic static String leftPad(String str, int size, char padChar)
Left pad a String with a specified character.
Pad to a size of size
.
ManStringUtil.leftPad(null, *, *) = null ManStringUtil.leftPad("", 3, 'z') = "zzz" ManStringUtil.leftPad("bat", 3, 'z') = "bat" ManStringUtil.leftPad("bat", 5, 'z') = "zzbat" ManStringUtil.leftPad("bat", 1, 'z') = "bat" ManStringUtil.leftPad("bat", -1, 'z') = "bat"
str
- the String to pad out, may be nullsize
- the size to pad topadChar
- the character to pad withnull
if null String inputpublic static String leftPad(String str, int size, String padStr)
Left pad a String with a specified String.
Pad to a size of size
.
ManStringUtil.leftPad(null, *, *) = null ManStringUtil.leftPad("", 3, "z") = "zzz" ManStringUtil.leftPad("bat", 3, "yz") = "bat" ManStringUtil.leftPad("bat", 5, "yz") = "yzbat" ManStringUtil.leftPad("bat", 8, "yz") = "yzyzybat" ManStringUtil.leftPad("bat", 1, "yz") = "bat" ManStringUtil.leftPad("bat", -1, "yz") = "bat" ManStringUtil.leftPad("bat", 5, null) = " bat" ManStringUtil.leftPad("bat", 5, "") = " bat"
str
- the String to pad out, may be nullsize
- the size to pad topadStr
- the String to pad with, null or empty treated as single spacenull
if null String inputpublic static int length(String str)
0
if the String is null
.str
- a String or null
0
if the String is null
.public static String center(String str, 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.
A null
String returns null
.
A negative size is treated as zero.
Equivalent to center(str, size, " ")
.
ManStringUtil.center(null, *) = null ManStringUtil.center("", 4) = " " ManStringUtil.center("ab", -1) = "ab" ManStringUtil.center("ab", 4) = " ab " ManStringUtil.center("abcd", 2) = "abcd" ManStringUtil.center("a", 4) = " a "
str
- the String to center, may be nullsize
- the int size of new String, negative treated as zeronull
if null String inputpublic static String center(String str, int size, char padChar)
Centers a String in a larger String of size size
.
Uses a supplied character as the value to pad the String with.
If the size is less than the String length, the String is returned.
A null
String returns null
.
A negative size is treated as zero.
ManStringUtil.center(null, *, *) = null ManStringUtil.center("", 4, ' ') = " " ManStringUtil.center("ab", -1, ' ') = "ab" ManStringUtil.center("ab", 4, ' ') = " ab" ManStringUtil.center("abcd", 2, ' ') = "abcd" ManStringUtil.center("a", 4, ' ') = " a " ManStringUtil.center("a", 4, 'y') = "yayy"
str
- the String to center, may be nullsize
- the int size of new String, negative treated as zeropadChar
- the character to pad the new String withnull
if null String inputpublic static String center(String str, int size, String padStr)
Centers a String in a larger String of size size
.
Uses a supplied String as the value to pad the String with.
If the size is less than the String length, the String is returned.
A null
String returns null
.
A negative size is treated as zero.
ManStringUtil.center(null, *, *) = null ManStringUtil.center("", 4, " ") = " " ManStringUtil.center("ab", -1, " ") = "ab" ManStringUtil.center("ab", 4, " ") = " ab" ManStringUtil.center("abcd", 2, " ") = "abcd" ManStringUtil.center("a", 4, " ") = " a " ManStringUtil.center("a", 4, "yz") = "yayz" ManStringUtil.center("abc", 7, null) = " abc " ManStringUtil.center("abc", 7, "") = " abc "
str
- the String to center, may be nullsize
- the int size of new String, negative treated as zeropadStr
- the String to pad the new String with, must not be null or emptynull
if null String inputIllegalArgumentException
- if padStr is null
or emptypublic static String upperCase(String str)
Converts a String to upper case as per String.toUpperCase()
.
A null
input String returns null
.
ManStringUtil.upperCase(null) = null ManStringUtil.upperCase("") = "" ManStringUtil.upperCase("aBc") = "ABC"
Note: As described in the documentation for String.toUpperCase()
,
the result of this method is affected by the current locale.
For platform-independent case transformations, the method lowerCase(String, Locale)
should be used with a specific locale (e.g. Locale.ENGLISH
).
str
- the String to upper case, may be nullnull
if null String inputpublic static String upperCase(String str, Locale locale)
Converts a String to upper case as per String.toUpperCase(Locale)
.
A null
input String returns null
.
ManStringUtil.upperCase(null, Locale.ENGLISH) = null ManStringUtil.upperCase("", Locale.ENGLISH) = "" ManStringUtil.upperCase("aBc", Locale.ENGLISH) = "ABC"
str
- the String to upper case, may be nulllocale
- the locale that defines the case transformation rules, must not be nullnull
if null String inputpublic static String lowerCase(String str)
Converts a String to lower case as per String.toLowerCase()
.
A null
input String returns null
.
ManStringUtil.lowerCase(null) = null ManStringUtil.lowerCase("") = "" ManStringUtil.lowerCase("aBc") = "abc"
Note: As described in the documentation for String.toLowerCase()
,
the result of this method is affected by the current locale.
For platform-independent case transformations, the method lowerCase(String, Locale)
should be used with a specific locale (e.g. Locale.ENGLISH
).
str
- the String to lower case, may be nullnull
if null String inputpublic static String lowerCase(String str, Locale locale)
Converts a String to lower case as per String.toLowerCase(Locale)
.
A null
input String returns null
.
ManStringUtil.lowerCase(null, Locale.ENGLISH) = null ManStringUtil.lowerCase("", Locale.ENGLISH) = "" ManStringUtil.lowerCase("aBc", Locale.ENGLISH) = "abc"
str
- the String to lower case, may be nulllocale
- the locale that defines the case transformation rules, must not be nullnull
if null String inputpublic static String capitalize(String str)
Capitalizes a String changing the first letter to title case as
per Character.toTitleCase(char)
. No other letters are changed.
ManStringUtil.capitalize(null) = null ManStringUtil.capitalize("") = "" ManStringUtil.capitalize("cat") = "Cat" ManStringUtil.capitalize("cAt") = "CAt"
str
- the String to capitalize, may be nullnull
if null String inputuncapitalize(String)
public static String capitalise(String str)
capitalize(String)
.
Method will be removed in Commons Lang 3.0.Capitalizes a String changing the first letter to title case as
per Character.toTitleCase(char)
. No other letters are changed.
str
- the String to capitalize, may be nullnull
if null String inputpublic static String uncapitalize(String str)
Uncapitalizes a String changing the first letter to title case as
per Character.toLowerCase(char)
. No other letters are changed.
ManStringUtil.uncapitalize(null) = null ManStringUtil.uncapitalize("") = "" ManStringUtil.uncapitalize("Cat") = "cat" ManStringUtil.uncapitalize("CAT") = "cAT"
str
- the String to uncapitalize, may be nullnull
if null String inputcapitalize(String)
public static String uncapitalise(String str)
uncapitalize(String)
.
Method will be removed in Commons Lang 3.0.Uncapitalizes a String changing the first letter to title case as
per Character.toLowerCase(char)
. No other letters are changed.
str
- the String to uncapitalize, may be nullnull
if null String inputpublic 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.
ManStringUtil.swapCase(null) = null ManStringUtil.swapCase("") = "" ManStringUtil.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
NOTE: This method changed in Lang version 2.0. It no longer performs a word based algorithm. If you only use ASCII, you will notice no change. That functionality is available in WordUtils.
str
- the String to swap case, may be nullnull
if null String inputpublic static int countMatches(String str, String sub)
Counts how many times the substring appears in the larger String.
A null
or empty ("") String input returns 0
.
ManStringUtil.countMatches(null, *) = 0 ManStringUtil.countMatches("", *) = 0 ManStringUtil.countMatches("abba", null) = 0 ManStringUtil.countMatches("abba", "") = 0 ManStringUtil.countMatches("abba", "a") = 2 ManStringUtil.countMatches("abba", "ab") = 1 ManStringUtil.countMatches("abba", "xxx") = 0
str
- the String to check, may be nullsub
- the substring to count, may be nullnull
public static int countRegexpMatches(String str, String regexp)
Counts how many times the regexp appears in the larger String.
A null
or empty ("") String input returns 0
.
ManStringUtil.countMatches(null, *) = 0 ManStringUtil.countMatches("", *) = 0 ManStringUtil.countMatches("abba", null) = 0 ManStringUtil.countMatches("abba", "") = 0 ManStringUtil.countMatches("abba", "a") = 2 ManStringUtil.countMatches("abba", "ab") = 1 ManStringUtil.countMatches("abba", ".b") = 2 ManStringUtil.countMatches("abba", "xxx") = 0
str
- the String to check, may be nullregexp
- the regexp to count, may be nullnull
public static boolean isAlpha(String str)
Checks if the String contains only unicode letters.
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isAlpha(null) = false ManStringUtil.isAlpha("") = true ManStringUtil.isAlpha(" ") = false ManStringUtil.isAlpha("abc") = true ManStringUtil.isAlpha("ab2c") = false ManStringUtil.isAlpha("ab-c") = false
str
- the String to check, may be nulltrue
if only contains letters, and is non-nullpublic static boolean isAlphaSpace(String str)
Checks if the String contains only unicode letters and space (' ').
null
will return false
An empty String ("") will return true
.
ManStringUtil.isAlphaSpace(null) = false ManStringUtil.isAlphaSpace("") = true ManStringUtil.isAlphaSpace(" ") = true ManStringUtil.isAlphaSpace("abc") = true ManStringUtil.isAlphaSpace("ab c") = true ManStringUtil.isAlphaSpace("ab2c") = false ManStringUtil.isAlphaSpace("ab-c") = false
str
- the String to check, may be nulltrue
if only contains letters and space,
and is non-nullpublic static boolean isAlphanumeric(String str)
Checks if the String contains only unicode letters or digits.
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isAlphanumeric(null) = false ManStringUtil.isAlphanumeric("") = true ManStringUtil.isAlphanumeric(" ") = false ManStringUtil.isAlphanumeric("abc") = true ManStringUtil.isAlphanumeric("ab c") = false ManStringUtil.isAlphanumeric("ab2c") = true ManStringUtil.isAlphanumeric("ab-c") = false
str
- the String to check, may be nulltrue
if only contains letters or digits,
and is non-nullpublic static boolean isAlphanumericSpace(String str)
Checks if the String contains only unicode letters, digits
or space (' '
).
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isAlphanumeric(null) = false ManStringUtil.isAlphanumeric("") = true ManStringUtil.isAlphanumeric(" ") = true ManStringUtil.isAlphanumeric("abc") = true ManStringUtil.isAlphanumeric("ab c") = true ManStringUtil.isAlphanumeric("ab2c") = true ManStringUtil.isAlphanumeric("ab-c") = false
str
- the String to check, may be nulltrue
if only contains letters, digits or space,
and is non-nullpublic static boolean isNumeric(String str)
Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isNumeric(null) = false ManStringUtil.isNumeric("") = true ManStringUtil.isNumeric(" ") = false ManStringUtil.isNumeric("123") = true ManStringUtil.isNumeric("12 3") = false ManStringUtil.isNumeric("ab2c") = false ManStringUtil.isNumeric("12-3") = false ManStringUtil.isNumeric("12.3") = false
str
- the String to check, may be nulltrue
if only contains digits, and is non-nullpublic static boolean isNumericSpace(String str)
Checks if the String contains only unicode digits or space
(' '
).
A decimal point is not a unicode digit and returns false.
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isNumeric(null) = false ManStringUtil.isNumeric("") = true ManStringUtil.isNumeric(" ") = true ManStringUtil.isNumeric("123") = true ManStringUtil.isNumeric("12 3") = true ManStringUtil.isNumeric("ab2c") = false ManStringUtil.isNumeric("12-3") = false ManStringUtil.isNumeric("12.3") = false
str
- the String to check, may be nulltrue
if only contains digits or space,
and is non-nullpublic static boolean isHexidecimal(String str)
Checks if the String contains only hexidecimal digits. A decimal point is not a hexidecimal digit and returns false.
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isHexidecimal(null) = false ManStringUtil.isHexidecimal("") = true ManStringUtil.isHexidecimal(" ") = false ManStringUtil.isHexidecimal("123") = true ManStringUtil.isHexidecimal("12 3") = false ManStringUtil.isHexidecimal("ab2c") = true ManStringUtil.isHexidecimal("ah2c") = false ManStringUtil.isHexidecimal("12-3") = false ManStringUtil.isHexidecimal("12.3") = false
str
- the String to check, may be nulltrue
if only contains hexidecimal digits, and is non-nullpublic static boolean isHexidecimalSpace(String str)
Checks if the String contains only hexidecimal digits or space
(' '
).
A decimal point is not a hexidecimal digit and returns false.
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isHexidecimal(null) = false ManStringUtil.isHexidecimal("") = true ManStringUtil.isHexidecimal(" ") = true ManStringUtil.isHexidecimal("123") = true ManStringUtil.isHexidecimal("12 3") = true ManStringUtil.isHexidecimal("ab2c") = true ManStringUtil.isHexidecimal("ah2c") = false ManStringUtil.isHexidecimal("12-3") = false ManStringUtil.isHexidecimal("12.3") = false
str
- the String to check, may be nulltrue
if only contains hexidecimal digits or space,
and is non-nullpublic static boolean isWhitespace(String str)
Checks if the String contains only whitespace.
null
will return false
.
An empty String ("") will return true
.
ManStringUtil.isWhitespace(null) = false ManStringUtil.isWhitespace("") = true ManStringUtil.isWhitespace(" ") = true ManStringUtil.isWhitespace("abc") = false ManStringUtil.isWhitespace("ab2c") = false ManStringUtil.isWhitespace("ab-c") = false
str
- the String to check, may be nulltrue
if only contains whitespace, and is non-nullpublic static String defaultString(String str)
Returns either the passed in String,
or if the String is null
, an empty String ("").
ManStringUtil.defaultString(null) = "" ManStringUtil.defaultString("") = "" ManStringUtil.defaultString("bat") = "bat"
str
- the String to check, may be nullnull
String.valueOf(Object)
public static String defaultString(String str, String defaultStr)
Returns either the passed in String, or if the String is
null
, the value of defaultStr
.
ManStringUtil.defaultString(null, "NULL") = "NULL" ManStringUtil.defaultString("", "NULL") = "" ManStringUtil.defaultString("bat", "NULL") = "bat"
str
- the String to check, may be nulldefaultStr
- the default String to return
if the input is null
, may be nullnull
String.valueOf(Object)
public static String defaultIfEmpty(String str, String defaultStr)
Returns either the passed in String, or if the String is
empty or null
, the value of defaultStr
.
ManStringUtil.defaultIfEmpty(null, "NULL") = "NULL" ManStringUtil.defaultIfEmpty("", "NULL") = "NULL" ManStringUtil.defaultIfEmpty("bat", "NULL") = "bat" ManStringUtil.defaultIfEmpty("", null) = null
str
- the String to check, may be nulldefaultStr
- the default String to return
if the input is empty ("") or null
, may be nulldefaultString(String, String)
public static String reverse(String str)
Reverses a String as per StringBuilder.reverse()
.
A null
String returns null
.
ManStringUtil.reverse(null) = null ManStringUtil.reverse("") = "" ManStringUtil.reverse("bat") = "tab"
str
- the String to reverse, may be nullnull
if null String inputpublic static String abbreviate(String str, int maxWidth)
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "Now is the time for..."
Specifically:
str
is less than maxWidth
characters
long, return it.(substring(str, 0, max-3) + "...")
.maxWidth
is less than 4
, throw an
IllegalArgumentException
.maxWidth
.
ManStringUtil.abbreviate(null, *) = null ManStringUtil.abbreviate("", 4) = "" ManStringUtil.abbreviate("abcdefg", 6) = "abc..." ManStringUtil.abbreviate("abcdefg", 7) = "abcdefg" ManStringUtil.abbreviate("abcdefg", 8) = "abcdefg" ManStringUtil.abbreviate("abcdefg", 4) = "a..." ManStringUtil.abbreviate("abcdefg", 3) = IllegalArgumentException
str
- the String to check, may be nullmaxWidth
- maximum length of result String, must be at least 4null
if null String inputIllegalArgumentException
- if the width is too smallpublic static String abbreviate(String str, int offset, int maxWidth)
Abbreviates a String using ellipses. This will turn "Now is the time for all good men" into "...is the time for..."
Works like abbreviate(String, int)
, but allows you to specify
a "left edge" offset. Note that this left edge is not necessarily going to
be the leftmost character in the result, or the first character following the
ellipses, but it will appear somewhere in the result.
In no case will it return a String of length greater than
maxWidth
.
ManStringUtil.abbreviate(null, *, *) = null ManStringUtil.abbreviate("", 0, 4) = "" ManStringUtil.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..." ManStringUtil.abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..." ManStringUtil.abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..." ManStringUtil.abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..." ManStringUtil.abbreviate("abcdefghijklmno", 5, 10) = "...fghi..." ManStringUtil.abbreviate("abcdefghijklmno", 6, 10) = "...ghij..." ManStringUtil.abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno" ManStringUtil.abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno" ManStringUtil.abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno" ManStringUtil.abbreviate("abcdefghij", 0, 3) = IllegalArgumentException ManStringUtil.abbreviate("abcdefghij", 5, 6) = IllegalArgumentException
str
- the String to check, may be nulloffset
- left edge of source StringmaxWidth
- maximum length of result String, must be at least 4null
if null String inputIllegalArgumentException
- if the width is too smallpublic static String difference(String str1, String str2)
Compares two Strings, and returns the portion where they differ. (More precisely, return the remainder of the second String, starting from where it's different from the first.)
For example,
difference("i am a machine", "i am a robot") -> "robot"
.
ManStringUtil.difference(null, null) = null ManStringUtil.difference("", "") = "" ManStringUtil.difference("", "abc") = "abc" ManStringUtil.difference("abc", "") = "" ManStringUtil.difference("abc", "abc") = "" ManStringUtil.difference("ab", "abxyz") = "xyz" ManStringUtil.difference("abcde", "abxyz") = "xyz" ManStringUtil.difference("abcde", "xyz") = "xyz"
str1
- the first String, may be nullstr2
- the second String, may be nullpublic static int indexOfDifference(String str1, String str2)
Compares two Strings, and returns the index at which the Strings begin to differ.
For example,
indexOfDifference("i am a machine", "i am a robot") -> 7
ManStringUtil.indexOfDifference(null, null) = -1 ManStringUtil.indexOfDifference("", "") = -1 ManStringUtil.indexOfDifference("", "abc") = 0 ManStringUtil.indexOfDifference("abc", "") = 0 ManStringUtil.indexOfDifference("abc", "abc") = -1 ManStringUtil.indexOfDifference("ab", "abxyz") = 2 ManStringUtil.indexOfDifference("abcde", "abxyz") = 2 ManStringUtil.indexOfDifference("abcde", "xyz") = 0
str1
- the first String, may be nullstr2
- the second String, may be nullpublic static int indexOfDifference(String[] strs)
Compares all Strings in an array and returns the index at which the Strings begin to differ.
For example,
indexOfDifference(new String[] {"i am a machine", "i am a robot"}) -> 7
ManStringUtil.indexOfDifference(null) = -1 ManStringUtil.indexOfDifference(new String[] {}) = -1 ManStringUtil.indexOfDifference(new String[] {"abc"}) = -1 ManStringUtil.indexOfDifference(new String[] {null, null}) = -1 ManStringUtil.indexOfDifference(new String[] {"", ""}) = -1 ManStringUtil.indexOfDifference(new String[] {"", null}) = 0 ManStringUtil.indexOfDifference(new String[] {"abc", null, null}) = 0 ManStringUtil.indexOfDifference(new String[] {null, null, "abc"}) = 0 ManStringUtil.indexOfDifference(new String[] {"", "abc"}) = 0 ManStringUtil.indexOfDifference(new String[] {"abc", ""}) = 0 ManStringUtil.indexOfDifference(new String[] {"abc", "abc"}) = -1 ManStringUtil.indexOfDifference(new String[] {"abc", "a"}) = 1 ManStringUtil.indexOfDifference(new String[] {"ab", "abxyz"}) = 2 ManStringUtil.indexOfDifference(new String[] {"abcde", "abxyz"}) = 2 ManStringUtil.indexOfDifference(new String[] {"abcde", "xyz"}) = 0 ManStringUtil.indexOfDifference(new String[] {"xyz", "abcde"}) = 0 ManStringUtil.indexOfDifference(new String[] {"i am a machine", "i am a robot"}) = 7
strs
- array of strings, entries may be nullpublic static String getCommonPrefix(String[] strs)
Compares all Strings in an array and returns the demo 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 "
ManStringUtil.getCommonPrefix(null) = "" ManStringUtil.getCommonPrefix(new String[] {}) = "" ManStringUtil.getCommonPrefix(new String[] {"abc"}) = "abc" ManStringUtil.getCommonPrefix(new String[] {null, null}) = "" ManStringUtil.getCommonPrefix(new String[] {"", ""}) = "" ManStringUtil.getCommonPrefix(new String[] {"", null}) = "" ManStringUtil.getCommonPrefix(new String[] {"abc", null, null}) = "" ManStringUtil.getCommonPrefix(new String[] {null, null, "abc"}) = "" ManStringUtil.getCommonPrefix(new String[] {"", "abc"}) = "" ManStringUtil.getCommonPrefix(new String[] {"abc", ""}) = "" ManStringUtil.getCommonPrefix(new String[] {"abc", "abc"}) = "abc" ManStringUtil.getCommonPrefix(new String[] {"abc", "a"}) = "a" ManStringUtil.getCommonPrefix(new String[] {"ab", "abxyz"}) = "ab" ManStringUtil.getCommonPrefix(new String[] {"abcde", "abxyz"}) = "ab" ManStringUtil.getCommonPrefix(new String[] {"abcde", "xyz"}) = "" ManStringUtil.getCommonPrefix(new String[] {"xyz", "abcde"}) = "" ManStringUtil.getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) = "i am a "
strs
- array of String objects, entries may be nullpublic static int getLevenshteinDistance(String s, String 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
ManStringUtil.getLevenshteinDistance(null, *) = IllegalArgumentException ManStringUtil.getLevenshteinDistance(*, null) = IllegalArgumentException ManStringUtil.getLevenshteinDistance("","") = 0 ManStringUtil.getLevenshteinDistance("","a") = 1 ManStringUtil.getLevenshteinDistance("aaapppp", "") = 7 ManStringUtil.getLevenshteinDistance("frog", "fog") = 1 ManStringUtil.getLevenshteinDistance("fly", "ant") = 3 ManStringUtil.getLevenshteinDistance("elephant", "hippo") = 7 ManStringUtil.getLevenshteinDistance("hippo", "elephant") = 7 ManStringUtil.getLevenshteinDistance("hippo", "zzzzzzzz") = 8 ManStringUtil.getLevenshteinDistance("hello", "hallo") = 1
s
- the first String, must not be nullt
- the second String, must not be nullIllegalArgumentException
- if either String input null
public static boolean startsWith(String str, String prefix)
Check if a String starts with a specified prefix.
null
s are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
ManStringUtil.startsWith(null, null) = true ManStringUtil.startsWith(null, "abc") = false ManStringUtil.startsWith("abcdef", null) = false ManStringUtil.startsWith("abcdef", "abc") = true ManStringUtil.startsWith("ABCDEF", "abc") = false
str
- the String to check, may be nullprefix
- the prefix to find, may be nulltrue
if the String starts with the prefix, case sensitive, or
both null
String.startsWith(String)
public static boolean startsWithIgnoreCase(String str, String prefix)
Case insensitive check if a String starts with a specified prefix.
null
s are handled without exceptions. Two null
references are considered to be equal. The comparison is case insensitive.
ManStringUtil.startsWithIgnoreCase(null, null) = true ManStringUtil.startsWithIgnoreCase(null, "abc") = false ManStringUtil.startsWithIgnoreCase("abcdef", null) = false ManStringUtil.startsWithIgnoreCase("abcdef", "abc") = true ManStringUtil.startsWithIgnoreCase("ABCDEF", "abc") = true
str
- the String to check, may be nullprefix
- the prefix to find, may be nulltrue
if the String starts with the prefix, case insensitive, or
both null
String.startsWith(String)
public static boolean startsWithAny(String string, String[] searchStrings)
Check if a String starts with any of an array of specified strings.
ManStringUtil.startsWithAny(null, null) = false ManStringUtil.startsWithAny(null, new String[] {"abc"}) = false ManStringUtil.startsWithAny("abcxyz", null) = false ManStringUtil.startsWithAny("abcxyz", new String[] {""}) = false ManStringUtil.startsWithAny("abcxyz", new String[] {"abc"}) = true ManStringUtil.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
string
- the String to check, may be nullsearchStrings
- the Strings to find, may be null or emptytrue
if the String starts with any of the the prefixes, case insensitive, or
both null
public static boolean endsWith(String str, String suffix)
Check if a String ends with a specified suffix.
null
s are handled without exceptions. Two null
references are considered to be equal. The comparison is case sensitive.
ManStringUtil.endsWith(null, null) = true ManStringUtil.endsWith(null, "def") = false ManStringUtil.endsWith("abcdef", null) = false ManStringUtil.endsWith("abcdef", "def") = true ManStringUtil.endsWith("ABCDEF", "def") = false ManStringUtil.endsWith("ABCDEF", "cde") = false
str
- the String to check, may be nullsuffix
- the suffix to find, may be nulltrue
if the String ends with the suffix, case sensitive, or
both null
String.endsWith(String)
public static boolean endsWithIgnoreCase(String str, String suffix)
Case insensitive check if a String ends with a specified suffix.
null
s are handled without exceptions. Two null
references are considered to be equal. The comparison is case insensitive.
ManStringUtil.endsWithIgnoreCase(null, null) = true ManStringUtil.endsWithIgnoreCase(null, "def") = false ManStringUtil.endsWithIgnoreCase("abcdef", null) = false ManStringUtil.endsWithIgnoreCase("abcdef", "def") = true ManStringUtil.endsWithIgnoreCase("ABCDEF", "def") = true ManStringUtil.endsWithIgnoreCase("ABCDEF", "cde") = false
str
- the String to check, may be nullsuffix
- the suffix to find, may be nulltrue
if the String ends with the suffix, case insensitive, or
both null
String.endsWith(String)
public static boolean isAsciiAlphanumeric(char ch)
Checks whether the character is ASCII 7 bit numeric.
CharUtils.isAsciiAlphanumeric('a') = true CharUtils.isAsciiAlphanumeric('A') = true CharUtils.isAsciiAlphanumeric('3') = true CharUtils.isAsciiAlphanumeric('-') = false CharUtils.isAsciiAlphanumeric('\n') = false CharUtils.isAsciiAlphanumeric('©') = false
ch
- the character to checkpublic static RegExpMatch match(String str, String regExp)
public static int getLineNumberForIndex(String strSource, int iIndex)
public static int getIndexForLineNumber(String strSource, int iLine)
public static String toCamelCase(String thiz)
Copyright © 2024. All rights reserved.