public class StringUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
EMPTY_STRING |
static char[] |
HEX_CHAR |
static int |
INDEX_NOT_FOUND |
| Constructor and Description |
|---|
StringUtils() |
| Modifier and Type | Method and Description |
|---|---|
static int |
compareVersions(String version1,
String version2)
Compares two version name strings using maven's ComparableVersion class.
|
static String |
concatenateWithThreshold(String a,
String b,
int byteLengthThreshold)
Concatenates two strings such that the total byte length does not exceed the threshold.
|
static String |
emptyToNull(String string)
Returns the given string if it is nonempty;
null otherwise. |
static char[] |
encodeHex(byte[] data) |
static String |
fromUTF8Bytes(byte[] bytes) |
static String |
fromUTF8Bytes(byte[] bytes,
int offset,
int length) |
static String |
getSuffixBy(String input,
int ch) |
static byte[] |
getUTF8Bytes(String str) |
static boolean |
isNullOrEmpty(String str) |
static String |
join(CharSequence delimiter,
Iterable<? extends CharSequence> elements)
Wrapper of
String.join(CharSequence, Iterable). |
static String |
join(List<String> list,
String separator) |
static <K,V> String |
join(Map<K,V> map) |
static <T> String |
join(String... elements)
Joins the elements of the provided array into a single String containing the provided list of elements.
|
static String |
join(String[] array,
String separator) |
static <T> String |
joinUsingDelim(String delim,
String... elements) |
static boolean |
nonEmpty(String str) |
static String |
nullToEmpty(String string)
Returns the given string if it is non-null; the empty string otherwise.
|
static String |
objToString(Object obj) |
static String |
removeSuffixBy(String input,
int ch) |
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 List<String> |
split(String input,
String delimiter)
Splits input string, delimited
delimiter into a list of non-empty strings
(skipping any empty string produced during splitting) |
static String |
stripEnd(String str,
String stripChars)
Strips given characters from end of the string and returns the result
|
static String |
toHexString(byte[] bytes) |
static <T> String |
toStringWithThreshold(List<T> objectList,
int lengthThreshold)
Concatenates the string representation of each object in the list
and returns a single string.
|
static String |
truncate(String str,
int headLength,
int tailLength) |
public static final char[] HEX_CHAR
public static final String EMPTY_STRING
public static final int INDEX_NOT_FOUND
public static <T> String join(String... elements)
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.
StringUtils.join(null) = null StringUtils.join([]) = "" StringUtils.join([null]) = "" StringUtils.join(["a", "b", "c"]) = "abc" StringUtils.join([null, "", "a"]) = "a"
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
public static String toHexString(byte[] bytes)
public static char[] encodeHex(byte[] data)
public static byte[] getUTF8Bytes(String str)
public static String fromUTF8Bytes(byte[] bytes)
public static String fromUTF8Bytes(byte[] bytes, int offset, int length)
public static boolean isNullOrEmpty(String str)
public static boolean nonEmpty(String str)
public static String nullToEmpty(@Nullable String string)
string - the string to test and possibly returnstring itself if it is non-null; "" if it is null@Nullable public static String emptyToNull(@Nullable String string)
null otherwise.string - the string to test and possibly returnstring itself if it is nonempty; null if it is empty or nullpublic static List<String> split(@Nullable String input, String delimiter)
delimiter into a list of non-empty strings
(skipping any empty string produced during splitting)public static int compareVersions(String version1, String version2)
version1 - the first version to compareversion2 - the second version to comparepublic 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"
This method is copied from hadoop StringUtils.
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"
This method is copied from hadoop StringUtils.
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 stripEnd(String str, String stripChars)
str - - string to be strippedstripChars - - characters to strippublic static String concatenateWithThreshold(String a, String b, int byteLengthThreshold)
a - The first stringb - The second stringbyteLengthThreshold - The maximum byte lengthpublic static <T> String toStringWithThreshold(List<T> objectList, int lengthThreshold)
T - type of the objectobjectList - object listlengthThreshold - number of characters to cap the stringCopyright © 2025 The Apache Software Foundation. All rights reserved.