public class StringUtils extends Object
BinaryString
and utility class to convert objects into strings in vice-versa.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 |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
bytesToBinaryString(byte[] bytes)
Given an array of bytes it will convert the bytes to a binary (0-1) string representation of
the bytes.
|
static String |
byteToHexString(byte[] bytes)
Given an array of bytes it will convert the bytes to a hex string representation of the
bytes.
|
static String |
byteToHexString(byte[] bytes,
int start,
int end)
Given an array of bytes it will convert the bytes to a hex string representation of the
bytes.
|
static String |
caseSensitiveConversion(String str,
boolean allowUpperCase) |
static BinaryString |
concat(BinaryString... inputs)
Concatenates input strings together into a single string.
|
static BinaryString |
concat(Iterable<BinaryString> inputs) |
static String |
getRandomString(Random rnd,
int minLength,
int maxLength)
Creates a random string with a length within the given interval.
|
static String |
getRandomString(Random rnd,
int minLength,
int maxLength,
char minValue,
char maxValue)
Creates a random string with a length within the given interval.
|
static boolean |
isEmpty(CharSequence cs)
Checks if a CharSequence is empty ("") or null.
|
static boolean |
isNullOrWhitespaceOnly(String str)
Checks if the string is null, empty, or contains only whitespace characters.
|
static boolean |
isNumeric(CharSequence cs) |
static String |
join(Iterable<?> iterable,
String separator)
Joins the elements of the provided
Iterable 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 |
quote(String str) |
static String |
randomNumericString(int len) |
static String |
repeat(String string,
int count)
Returns a string consisting of a specific number of concatenated copies of an input 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[] |
split(String str,
String separatorChars)
Splits the provided text into an array, separators specified.
|
static String[] |
split(String str,
String separatorChars,
int max,
boolean preserveAllTokens)
Performs the logic for the
split and splitPreserveAllTokens methods that
return a maximum array length. |
public static final int INDEX_NOT_FOUND
public static final String EMPTY
""
.public static BinaryString concat(BinaryString... inputs)
public static BinaryString concat(Iterable<BinaryString> inputs)
public static boolean isNullOrWhitespaceOnly(String str)
Character.isWhitespace(char)
.str
- The string to checkpublic static String byteToHexString(byte[] bytes, int start, int end)
bytes
- the bytes to convert in a hex stringstart
- start index, inclusivelyend
- end index, exclusivelypublic static String byteToHexString(byte[] bytes)
bytes
- the bytes to convert in a hex stringpublic static String bytesToBinaryString(byte[] bytes)
bytes
- the bytes to be convertedpublic static String getRandomString(Random rnd, int minLength, int maxLength)
rnd
- The random used to create the strings.minLength
- The minimum string length.maxLength
- The maximum string length (inclusive).public static String getRandomString(Random rnd, int minLength, int maxLength, char minValue, char maxValue)
rnd
- The random used to create the strings.minLength
- The minimum string length.maxLength
- The maximum string length (inclusive).minValue
- The minimum character value to occur.maxValue
- The maximum character value to occur.public static String repeat(String string, int count)
repeat("hey", 3)
returns the string "heyheyhey"
.string
- any non-null stringcount
- the number of times to repeat it; a nonnegative integerstring
repeated count
times (the empty string if
count
is zero)IllegalArgumentException
- if count
is negativepublic static String replace(String text, String searchString, String replacement)
A null
reference passed to this method is a no-op.
StringUtils.replace(null, *, *) = null StringUtils.replace("", *, *) = "" StringUtils.replace("any", null, *) = "any" StringUtils.replace("any", *, null) = "any" StringUtils.replace("any", "", *) = "any" StringUtils.replace("aba", "a", null) = "aba" StringUtils.replace("aba", "a", "") = "b" StringUtils.replace("aba", "a", "z") = "zbz"
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)
max
values of the search String.
A null
reference passed to this method is a no-op.
StringUtils.replace(null, *, *, *) = null StringUtils.replace("", *, *, *) = "" StringUtils.replace("any", null, *, *) = "any" StringUtils.replace("any", *, null, *) = "any" StringUtils.replace("any", "", *, *) = "any" StringUtils.replace("any", *, *, 0) = "any" StringUtils.replace("abaa", "a", null, -1) = "abaa" StringUtils.replace("abaa", "a", "", -1) = "b" StringUtils.replace("abaa", "a", "z", 0) = "abaa" StringUtils.replace("abaa", "a", "z", 1) = "zbaa" StringUtils.replace("abaa", "a", "z", 2) = "zbza" StringUtils.replace("abaa", "a", "z", -1) = "zbzz"
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 boolean isEmpty(CharSequence cs)
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 CharSequence. That functionality is available in isNullOrWhitespaceOnly().
cs
- the CharSequence to check, may be nulltrue
if the CharSequence is empty or nullpublic static String randomNumericString(int len)
public static String[] split(String str, String separatorChars)
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.
StringUtils.split(null, *) = null StringUtils.split("", *) = [] StringUtils.split("abc def", null) = ["abc", "def"] StringUtils.split("abc def", " ") = ["abc", "def"] StringUtils.split("abc def", " ") = ["abc", "def"] StringUtils.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, boolean preserveAllTokens)
split
and splitPreserveAllTokens
methods that
return a maximum array length.str
- the String to parse, may be null
separatorChars
- the separate charactermax
- the maximum number of elements to include in the array. A zero or negative value
implies no limit.preserveAllTokens
- if true
, adjacent separators are treated as empty token
separators; if false
, adjacent separators are treated as one separator.null
if null String inputpublic static String join(Iterable<?> iterable, String separator)
Iterable
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 ("").
iterable
- the Iterable
providing the values to join together, may be nullseparator
- the separator character to use, null treated as ""null
if null iterator inputpublic static String join(Iterator<?> iterator, String separator)
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 ("").
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 caseSensitiveConversion(String str, boolean allowUpperCase)
public static boolean isNumeric(CharSequence cs)
Copyright © 2023–2024 The Apache Software Foundation. All rights reserved.