public class StringUtils extends Object
Common String
manipulation routines.
Originally from Turbine and the GenerationJavaCore library.
Constructor and Description |
---|
StringUtils()
StringUtils instances should NOT be constructed in standard programming. |
Modifier and Type | Method and Description |
---|---|
static String |
abbreviate(String s,
int maxWidth) |
static String |
abbreviate(String s,
int offset,
int maxWidth) |
static String |
addAndDeHump(String view) |
static String |
capitalise(String str)
Capitalise a String.
|
static String |
capitaliseAllWords(String str)
Capitalise all the words in a String.
|
static String |
capitalizeFirstLetter(String data) |
static String |
center(String str,
int size)
Center a String in a larger String of size
n . |
static String |
center(String str,
int size,
String delim)
Center a String in a larger String of size
n . |
static String |
chomp(String str)
Remove the last newline, and everything after it from a String.
|
static String |
chomp(String str,
String sep)
Remove the last value of a supplied String, and everything after it from a String.
|
static String |
chompLast(String str)
Remove a newline if and only if it is at the end of the supplied String.
|
static String |
chompLast(String str,
String sep)
Remove a value if and only if the String ends with that value.
|
static String |
chop(String str)
Remove the last character from a String.
|
static String |
chopNewline(String str)
Remove
\n from end of a String if it's there. |
static String |
clean(String str)
Removes control characters, including whitespace, from both ends of this String, handling
null by
returning an empty String. |
static String |
concatenate(Object[] array)
Concatenates elements of an array into a single String.
|
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 int |
countMatches(String str,
String sub)
How many times is the substring in the larger String.
|
static String |
defaultString(Object obj)
Deprecated.
|
static String |
defaultString(Object obj,
String defaultString)
Deprecated.
|
static String |
deleteWhitespace(String str)
Deletes all whitespaces from a String.
|
static String |
difference(String s1,
String s2)
Compare two strings, and return the portion where they differ.
|
static int |
differenceAt(String s1,
String s2)
Compare two strings, and return the index at which the strings begin to differ.
|
static boolean |
equals(String str1,
String str2)
Deprecated.
|
static boolean |
equalsIgnoreCase(String str1,
String str2)
Compares two Strings, returning
true if they are equal ignoring the case. |
static String |
escape(String str)
Escapes any values it finds into their String form.
|
static String |
escape(String source,
char[] escapedChars,
char escapeChar) |
static String |
escape(String source,
char[] escapedChars,
String escapePattern) |
static String |
getChomp(String str,
String sep)
Remove everything and return the last value of a supplied String, and everything after it from a String.
|
static String |
getNestedString(String str,
String tag)
Get the String that is nested in between two instances of the same String.
|
static String |
getNestedString(String str,
String open,
String close)
Get the String that is nested in between two Strings.
|
static String |
getPrechomp(String str,
String sep)
Remove and return everything before the first value of a supplied String from another String.
|
static int |
indexOfAny(String str,
String[] searchStrs)
Find the first index of any of a set of potential substrings.
|
static String |
interpolate(String text,
Map<?,?> namespace) |
static boolean |
isAlpha(String str)
Checks if the String contains only unicode letters.
|
static boolean |
isAlphanumeric(String str)
Checks if the String contains only unicode letters or digits.
|
static boolean |
isAlphanumericSpace(String str)
Checks if the String contains only unicode letters, digits or space (
' ' ). |
static boolean |
isAlphaSpace(String str)
Checks if the String contains only unicode letters and space (
' ' ). |
static boolean |
isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
|
static boolean |
isEmpty(String str)
Checks if a (trimmed) String is
null or empty. |
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 non
null and is not empty (length > 0 ). |
static boolean |
isNumeric(String str)
Checks if the String contains only unicode digits.
|
static boolean |
isNumericSpace(String str)
Checks if the String contains only unicode digits or space (
' ' ). |
static boolean |
isWhitespace(String str)
Checks if the String contains only whitespace.
|
static String |
join(Iterator<?> iterator,
String separator)
Joins the elements of the provided
Iterator into a single String containing the provided elements. |
static String |
join(Object[] array,
String separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
|
static int |
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
n characters of a String. |
static String |
leftPad(String str,
int size)
Left pad a String with spaces.
|
static String |
leftPad(String str,
int size,
String delim)
Left pad a String with a specified string.
|
static String |
lowerCase(String str)
Convert a String to lower case,
null String returns null . |
static String |
lowercaseFirstLetter(String data) |
static String |
mid(String str,
int pos,
int len)
Gets
n characters from the middle of a String. |
static String |
overlayString(String text,
String overlay,
int start,
int end)
Overlay a part of a String with another String.
|
static String |
prechomp(String str,
String sep)
Remove the first value of a supplied String, and everything before it from a String.
|
static String |
quoteAndEscape(String source,
char quoteChar)
Quote and escape a String with the given character, handling
null . |
static String |
quoteAndEscape(String source,
char quoteChar,
char[] quotingTriggers)
Quote and escape a String with the given character, handling
null . |
static String |
quoteAndEscape(String source,
char quoteChar,
char[] escapedChars,
char[] quotingTriggers,
char escapeChar,
boolean force) |
static String |
quoteAndEscape(String source,
char quoteChar,
char[] escapedChars,
char[] quotingTriggers,
String escapePattern,
boolean force) |
static String |
quoteAndEscape(String source,
char quoteChar,
char[] escapedChars,
char escapeChar,
boolean force) |
static String |
removeAndHump(String data,
String replaceThis) |
static String |
removeDuplicateWhitespace(String s)
Remove all duplicate whitespace characters and line terminators are replaced with a single space.
|
static String |
repeat(String str,
int repeat)
Repeat a String
n times to form a new string. |
static String |
replace(String text,
char repl,
char with)
Replace all occurrences of a char within another char.
|
static String |
replace(String text,
char repl,
char with,
int max)
Replace a char with another char inside a larger String, for the first
max values of the search
char. |
static String |
replace(String text,
String repl,
String with)
Replace all occurrences of a String within another String.
|
static String |
replace(String text,
String repl,
String with,
int max)
Replace a String with another String inside a larger String, for the first
max values of the search
String. |
static String |
replaceOnce(String text,
char repl,
char with)
Replace a char with another char inside a larger String, once.
|
static String |
replaceOnce(String text,
String repl,
String with)
Replace a String with another String inside a larger String, once.
|
static String |
reverse(String str)
Reverse a String.
|
static String |
reverseDelimitedString(String str,
String delimiter)
Reverses a String that is delimited by a specific character.
|
static String |
right(String str,
int len)
Gets the rightmost
n characters of a String. |
static String |
rightPad(String str,
int size)
Right pad a String with spaces.
|
static String |
rightPad(String str,
int size,
String delim)
Right pad a String with a specified string.
|
static String[] |
split(String str)
Splits the provided text into a array, using whitespace as the separator.
|
static String[] |
split(String text,
String separator) |
static String[] |
split(String str,
String separator,
int max)
Splits the provided text into a array, based on a given separator.
|
static String |
strip(String str)
Remove whitespace from the front and back of a String.
|
static String |
strip(String str,
String delim)
Remove a specified String from the front and back of a String.
|
static String[] |
stripAll(String[] strs)
Strip whitespace from the front and back of every String in the array.
|
static String[] |
stripAll(String[] strs,
String delimiter)
Strip the specified delimiter from the front and back of every String in the array.
|
static String |
stripEnd(String str,
String strip)
Strip any of a supplied String from the end of a String.
|
static String |
stripStart(String str,
String strip)
Strip any of a supplied String from the start of a String.
|
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 |
swapCase(String str)
Swaps the case of String.
|
static String |
trim(String str)
Removes control characters, including whitespace, from both ends of this String, handling
null by
returning null . |
static String |
uncapitalise(String str)
Uncapitalise a String.
|
static String |
uncapitaliseAllWords(String str)
Uncapitalise all the words in a string.
|
static String |
unifyLineSeparators(String s)
Parses the given String and replaces all occurrences of '\n', '\r' and '\r\n' with the system line separator.
|
static String |
unifyLineSeparators(String s,
String ls)
Parses the given String and replaces all occurrences of '\n', '\r' and '\r\n' with the system line separator.
|
static String |
upperCase(String str)
Convert a String to upper case,
null String returns null . |
public StringUtils()
StringUtils
instances should NOT be constructed in standard programming. Instead, the class should
be used as StringUtils.trim(" foo ");
.
This constructor is public to permit tools that require a JavaBean manager to operate.
public static String clean(String str)
Removes control characters, including whitespace, from both ends of this String, handling null
by
returning an empty String.
str
- the String to checknull
)String.trim()
public static String trim(String str)
Removes control characters, including whitespace, from both ends of this String, handling null
by
returning null
.
str
- the String to checknull
)String.trim()
public static String deleteWhitespace(String str)
Deletes all whitespaces from a String.
Whitespace is defined by Character.isWhitespace(char)
.
str
- String target to delete whitespace frompublic static boolean isNotEmpty(String str)
Checks if a String is non null
and is not empty (length > 0
).
str
- the String to checkpublic static boolean isEmpty(String str)
Checks if a (trimmed) String is null
or empty.
Note: In future releases, this method will no longer trim the input string such that it works
complementary to isNotEmpty(String)
. Code that wants to test for whitespace-only strings should be
migrated to use isBlank(String)
instead.
str
- the String to checktrue
if the String is null
, or length zero once trimmedpublic static boolean isBlank(String str)
Checks if a String is whitespace, empty ("") or null.
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ") = true StringUtils.isBlank("bob") = false StringUtils.isBlank(" bob ") = false
str
- the String to check, may be 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.
StringUtils.isNotBlank(null) = false StringUtils.isNotBlank("") = false StringUtils.isNotBlank(" ") = false StringUtils.isNotBlank("bob") = true StringUtils.isNotBlank(" bob ") = true
str
- the String to check, may be nulltrue
if the String is not empty and not null and not whitespace@Deprecated public 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.
str1
- the first stringstr2
- the second stringtrue
if the Strings are equal, case sensitive, or both null
String.equals(Object)
,
Objects.equals(Object, Object)
public static boolean equalsIgnoreCase(String str1, String str2)
Compares two Strings, returning true
if they are equal ignoring the case.
Nulls
are handled without exceptions. Two null
references are considered equal.
Comparison is case insensitive.
str1
- the first stringstr2
- the second stringtrue
if the Strings are equal, case insensitive, or both null
String.equalsIgnoreCase(String)
public static int indexOfAny(String str, String[] searchStrs)
Find the first index of any of a set of potential substrings.
null
String will return -1
.
str
- the String to checksearchStrs
- the Strings to search forNullPointerException
- if any of searchStrs[i] is null
public static int lastIndexOfAny(String str, String[] searchStrs)
Find the latest index of any of a set of potential substrings.
null
string will return -1
.
str
- the String to checksearchStrs
- the Strings to search forNullPointerException
- if any of searchStrs[i] is null
public 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.
str
- the String to get the substring fromstart
- the position to start from, negative means count back from the end of the String by this many
characterspublic 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.
str
- the String to get the substring fromstart
- 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
characterspublic static String left(String str, int len)
Gets the leftmost n
characters of a String.
If n
characters are not available, or the String is null
, the String will be returned
without an exception.
str
- the String to get the leftmost characters fromlen
- the length of the required StringIllegalArgumentException
- if len is less than zeropublic static String right(String str, int len)
Gets the rightmost n
characters of a String.
If n
characters are not available, or the String is null
, the String will be returned
without an exception.
str
- the String to get the rightmost characters fromlen
- the length of the required StringIllegalArgumentException
- if len is less than zeropublic static String mid(String str, int pos, int len)
Gets n
characters from the middle of a String.
If n
characters are not available, the remainder of the String will be returned without an
exception. If the String is null
, null
will be returned.
str
- the String to get the characters frompos
- the position to start fromlen
- the length of the required StringIndexOutOfBoundsException
- if pos is out of boundsIllegalArgumentException
- if len is less than zeropublic static String[] split(String str)
Splits the provided text into a array, using whitespace as the separator.
The separator is not included in the returned String array.
str
- the String to parsepublic static String[] split(String text, String separator)
text
- The string to parse.separator
- Characters used as the delimiters. If null
, splits on whitespace.public static String[] split(String str, String separator, int max)
Splits the provided text into a array, based on a given separator.
The separator is not included in the returned String array. The maximum number of splits to perform can be
controlled. A null
separator will cause parsing to be on whitespace.
This is useful for quickly splitting a String directly into an array of tokens, instead of an enumeration of
tokens (as StringTokenizer
does).
str
- The string to parse.separator
- Characters used as the delimiters. If null
, splits on whitespace.max
- The maximum number of elements to include in the array. A zero or negative value implies no limit.public static String concatenate(Object[] array)
Concatenates elements of an array into a single String.
The difference from join is that concatenate has no delimiter.
array
- the array of values to concatenate.public 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 a blank String.
array
- the array of values to join togetherseparator
- the separator character to usepublic 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 a blank String.
iterator
- the Iterator
of values to join togetherseparator
- the separator character to usepublic static String replaceOnce(String text, char repl, char with)
Replace a char with another char inside a larger String, once.
A null
reference passed to this method is a no-op.
text
- text to search and replace inrepl
- char to search forwith
- char to replace withreplace(String text, char repl, char with, int max)
public static String replace(String text, char repl, char with)
Replace all occurrences of a char within another char.
A null
reference passed to this method is a no-op.
text
- text to search and replace inrepl
- char to search forwith
- char to replace withreplace(String text, char repl, char with, int max)
public static String replace(String text, char repl, char with, int max)
Replace a char with another char inside a larger String, for the first max
values of the search
char.
A null
reference passed to this method is a no-op.
text
- text to search and replace inrepl
- char to search forwith
- char to replace withmax
- maximum number of values to replace, or -1
if no maximumpublic static String replaceOnce(String text, String repl, String with)
Replace a String with another String inside a larger String, once.
A null
reference passed to this method is a no-op.
text
- text to search and replace inrepl
- String to search forwith
- String to replace withreplace(String text, String repl, String with, int max)
public static String replace(String text, String repl, String with)
Replace all occurrences of a String within another String.
A null
reference passed to this method is a no-op.
text
- text to search and replace inrepl
- String to search forwith
- String to replace withreplace(String text, String repl, String with, int max)
public static String replace(String text, String repl, String with, int max)
Replace 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.
text
- text to search and replace inrepl
- String to search forwith
- String to replace withmax
- maximum number of values to replace, or -1
if no maximumpublic static String overlayString(String text, String overlay, int start, int end)
Overlay a part of a String with another String.
text
- String to do overlaying inoverlay
- String to overlaystart
- int to start overlaying atend
- int to stop overlaying beforeNullPointerException
- if text or overlay is null
public static String center(String str, int size)
Center a String in a larger String of size n
.
Uses spaces as the value to buffer the String with. Equivalent to center(str, size, " ")
.
str
- String to centersize
- int size of new StringNullPointerException
- if str is null
public static String center(String str, int size, String delim)
Center a String in a larger String of size n
.
Uses a supplied String as the value to buffer the String with.
str
- String to centersize
- int size of new Stringdelim
- String to buffer the new String withNullPointerException
- if str or delim is null
ArithmeticException
- if delim is the empty Stringpublic static String chomp(String str)
Remove the last newline, and everything after it from a String.
str
- String to chomp the newline fromNullPointerException
- if str is null
public static String chomp(String str, String sep)
Remove the last value of a supplied String, and everything after it from a String.
str
- String to chomp fromsep
- String to chompNullPointerException
- if str or sep is null
public static String chompLast(String str)
Remove a newline if and only if it is at the end of the supplied String.
str
- String to chomp fromNullPointerException
- if str is null
public static String chompLast(String str, String sep)
Remove a value if and only if the String ends with that value.
str
- String to chomp fromsep
- String to chompNullPointerException
- if str or sep is null
public static String getChomp(String str, String sep)
Remove everything and return the last value of a supplied String, and everything after it from a String.
str
- String to chomp fromsep
- String to chompNullPointerException
- if str or sep is null
public static String prechomp(String str, String sep)
Remove the first value of a supplied String, and everything before it from a String.
str
- String to chomp fromsep
- String to chompNullPointerException
- if str or sep is null
public static String getPrechomp(String str, String sep)
Remove and return everything before the first value of a supplied String from another String.
str
- String to chomp fromsep
- String to chompNullPointerException
- 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.
str
- String to chop last character fromNullPointerException
- if str is null
public static String chopNewline(String str)
Remove \n
from end of a String if it's there. If a \r
precedes it, then remove that
too.
str
- String to chop a newline fromNullPointerException
- if str is null
public static String escape(String str)
Escapes any values it finds into their String form.
So a tab becomes the characters '\\'
and 't'
.
str
- String to escape values inNullPointerException
- if str is null
public static String repeat(String str, int repeat)
Repeat a String n
times to form a new string.
str
- String to repeatrepeat
- number of times to repeat strNegativeArraySizeException
- if repeat < 0
NullPointerException
- if str is null
public static String rightPad(String str, int size)
Right pad a String with spaces.
The String is padded to the size of n
.
str
- String to repeatsize
- number of times to repeat strNullPointerException
- if str is null
public static String rightPad(String str, int size, String delim)
Right pad a String with a specified string.
The String is padded to the size of n
.
str
- String to pad outsize
- size to pad todelim
- String to pad withNullPointerException
- if str or delim is null
ArithmeticException
- if delim is the empty Stringpublic static String leftPad(String str, int size)
Left pad a String with spaces.
The String is padded to the size of n
.
str
- String to pad outsize
- size to pad toNullPointerException
- if str or delim is null
public static String leftPad(String str, int size, String delim)
str
- String to pad outsize
- size to pad todelim
- String to pad withNullPointerException
- if str or delim is nullArithmeticException
- if delim is the empty stringpublic static String strip(String str)
Remove whitespace from the front and back of a String.
str
- the String to remove whitespace frompublic static String strip(String str, String delim)
Remove a specified String from the front and back of a String.
If whitespace is wanted to be removed, used the strip(java.lang.String)
method.
str
- the String to remove a string fromdelim
- the String to remove at start and endpublic static String[] stripAll(String[] strs)
Strip whitespace from the front and back of every String in the array.
strs
- the Strings to remove whitespace frompublic static String[] stripAll(String[] strs, String delimiter)
Strip the specified delimiter from the front and back of every String in the array.
strs
- the Strings to remove a String fromdelimiter
- the String to remove at start and endpublic static String stripEnd(String str, String strip)
Strip any of a supplied String from the end of a String.
If the strip String is null
, whitespace is stripped.
str
- the String to remove characters fromstrip
- the String to removepublic static String stripStart(String str, String strip)
Strip any of a supplied String from the start of a String.
If the strip String is null
, whitespace is stripped.
str
- the String to remove characters fromstrip
- the String to removepublic static String upperCase(String str)
Convert a String to upper case, null
String returns null
.
str
- the String to uppercasepublic static String lowerCase(String str)
Convert a String to lower case, null
String returns null
.
str
- the string to lowercasepublic static String uncapitalise(String str)
Uncapitalise a String.
That is, convert the first character into lower-case. null
is returned as null
.
str
- the String to uncapitalisepublic static String capitalise(String str)
Capitalise a String.
That is, convert the first character into title-case. null
is returned as null
.
str
- the String to capitalisepublic static String swapCase(String str)
Swaps the case of String.
Properly looks after making sure the start of words are Titlecase and not Uppercase.
null
is returned as null
.
str
- the String to swap the case ofpublic static String capitaliseAllWords(String str)
Capitalise all the words in a String.
Uses Character.isWhitespace(char)
as a separator between words.
null
will return null
.
str
- the String to capitalisepublic static String uncapitaliseAllWords(String str)
Uncapitalise all the words in a string.
Uses Character.isWhitespace(char)
as a separator between words.
null
will return null
.
str
- the string to uncapitalisepublic static String getNestedString(String str, String tag)
Get the String that is nested in between two instances of the same String.
If str
is null
, will return null
.
str
- the String containing nested-stringtag
- the String before and after nested-stringnull
NullPointerException
- if tag is null
public static String getNestedString(String str, String open, String close)
Get the String that is nested in between two Strings.
str
- the String containing nested-stringopen
- the String before nested-stringclose
- the String after nested-stringnull
NullPointerException
- if open or close is null
public static int countMatches(String str, String sub)
How many times is the substring in the larger String.
null
returns 0
.
str
- the String to checksub
- the substring to countnull
NullPointerException
- if sub is null
public static boolean isAlpha(String str)
Checks if the String contains only unicode letters.
null
will return false
. An empty String will return true
.
str
- the String to checktrue
if only contains letters, 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
.
str
- the String to checktrue
if only contains whitespace, 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
.
str
- the String to checktrue
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
.
str
- the String to checktrue
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
.
str
- the String to checktrue
if only contains letters, digits or space, and is non-nullpublic static boolean isNumeric(String str)
Checks if the String contains only unicode digits.
null
will return false
. An empty String will return true
.
str
- the String to checktrue
if only contains digits, and is non-nullpublic static boolean isNumericSpace(String str)
Checks if the String contains only unicode digits or space (' '
).
null
will return false
. An empty String will return true
.
str
- the String to checktrue
if only contains digits or space, and is non-null@Deprecated public static String defaultString(Object obj)
Returns either the passed in Object
as a String, or, if the Object
is
null
, an empty String.
obj
- the Object to checknull
Objects.toString(Object, String)
@Deprecated public static String defaultString(Object obj, String defaultString)
Returns either the passed in Object
as a String, or, if the Object
is
null
, a passed in default String.
obj
- the Object to checkdefaultString
- the default String to return if str is null
null
Objects.toString(Object, String)
public static String reverse(String str)
Reverse a String.
null
String returns null
.
str
- the String to reversepublic static String reverseDelimitedString(String str, String delimiter)
Reverses a String that is delimited by a specific character.
The Strings between the delimiters are not reversed. Thus java.lang.String becomes String.lang.java (if the
delimiter is '.'
).
str
- the String to reversedelimiter
- the delimiter to usepublic static String abbreviate(String s, int maxWidth)
s
- stringmaxWidth
- maximum length of result stringSpecifically:
If str is less than max characters long, return it. Else abbreviate it to (substring(str, 0, max-3) + "..."). If maxWidth is less than 3, throw an IllegalArgumentException. In no case will it return a string of length greater than maxWidth.
public static String abbreviate(String s, int offset, int maxWidth)
s
- stringoffset
- left edge of source stringmaxWidth
- maximum length of result stringpublic static String difference(String s1, String s2)
E.g. strdiff("i am a machine", "i am a robot") -> "robot"
s1
- strings2
- stringpublic static int differenceAt(String s1, String s2)
E.g. strdiff("i am a machine", "i am a robot") -> 7
s1
- strings2
- stringpublic static String quoteAndEscape(String source, char quoteChar)
Quote and escape a String with the given character, handling null
.
StringUtils.quoteAndEscape(null, *) = null StringUtils.quoteAndEscape("", *) = "" StringUtils.quoteAndEscape("abc", '"') = abc StringUtils.quoteAndEscape("a\"bc", '"') = "a\"bc" StringUtils.quoteAndEscape("a\"bc", '\'') = 'a\"bc'
source
- the source StringquoteChar
- the char used to quotequoteAndEscape(String, char, char[], char[], char, boolean)
public static String quoteAndEscape(String source, char quoteChar, char[] quotingTriggers)
Quote and escape a String with the given character, handling null
.
source
- the source StringquoteChar
- the char used to quotequotingTriggers
- chars generating a quotequoteAndEscape(String, char, char[], char[], char, boolean)
public static String quoteAndEscape(String source, char quoteChar, char[] escapedChars, char escapeChar, boolean force)
source
- the source StringquoteChar
- the char used to quoteescapedChars
- chars to escapeescapeChar
- char used for escapingforce
- force the quotingquoteAndEscape(String, char, char[], char[], char, boolean)
public static String quoteAndEscape(String source, char quoteChar, char[] escapedChars, char[] quotingTriggers, char escapeChar, boolean force)
source
- the source StringquoteChar
- the char used to quoteescapedChars
- chars to escapequotingTriggers
- chars generating a quoteescapeChar
- char used for escapingforce
- force the quotingpublic static String quoteAndEscape(String source, char quoteChar, char[] escapedChars, char[] quotingTriggers, String escapePattern, boolean force)
source
- the source StringquoteChar
- the char used to quoteescapedChars
- chars to escapequotingTriggers
- chars generating a quoteescapePattern
- pattern used for escapingforce
- force the quotingpublic static String escape(String source, char[] escapedChars, char escapeChar)
source
- the source StringescapedChars
- chars to escapeescapeChar
- char used for escapingpublic static String escape(String source, char[] escapedChars, String escapePattern)
source
- the source StringescapedChars
- chars to escapeescapePattern
- pattern used for escapingpublic static String removeDuplicateWhitespace(String s)
s
- a not null Stringpublic static String unifyLineSeparators(String s)
s
- a not null StringunifyLineSeparators(String, String)
public static String unifyLineSeparators(String s, String ls)
s
- a not null Stringls
- the wanted line separator ("\n" on UNIX), if null using the System line separator.IllegalArgumentException
- if ls is not '\n', '\r' and '\r\n' characters.public 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
.
StringUtils.contains(null, *) = false StringUtils.contains("", *) = false StringUtils.contains("abc", 'a') = true StringUtils.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(int)
.
A null
String will return false
.
StringUtils.contains(null, *) = false StringUtils.contains(*, null) = false StringUtils.contains("", "") = true StringUtils.contains("abc", "") = true StringUtils.contains("abc", "a") = true StringUtils.contains("abc", "z") = false
str
- the String to check, may be nullsearchStr
- the String to find, may be nullnull
string inputCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.