Package org.apache.flink.util
Class StringUtils
- java.lang.Object
-
- org.apache.flink.util.StringUtils
-
@PublicEvolving public final class StringUtils extends Object
Utility class to convert objects into strings in vice-versa.
-
-
Field Summary
Fields Modifier and Type Field Description static String[]
EMPTY_STRING_ARRAY
An empty string array.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
arrayAwareToString(Object o)
Converts the given object into a string representation by callingObject.toString()
and formatting (possibly nested) arrays andnull
.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
concatenateWithAnd(String s1, String s2)
If both string arguments are non-null, this method concatenates them with ' and '.static String
generateRandomAlphanumericString(Random rnd, int length)
Creates a random alphanumeric string of given length.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 byte[]
hexStringToByte(String hex)
Given a hex string this will return the byte array corresponding to the string .static boolean
isNullOrWhitespaceOnly(String str)
Checks if the string is null, empty, or contains only whitespace characters.static String
readNullableString(DataInputView in)
Reads a String from the given input.static String
readString(DataInputView in)
Reads a non-null String from the given input.static String
showControlCharacters(String str)
Replaces control characters by their escape-coded version.static String
toQuotedListString(Object[] values)
Generates a string containing a comma-separated list of values in double-quotes.static void
writeNullableString(String str, DataOutputView out)
Writes a String to the given output.static void
writeString(String str, DataOutputView out)
Writes a String to the given output.
-
-
-
Field Detail
-
EMPTY_STRING_ARRAY
public static final String[] EMPTY_STRING_ARRAY
An empty string array. There are just too many places where one needs an empty string array and wants to save some object allocation.
-
-
Method Detail
-
byteToHexString
public 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.- Parameters:
bytes
- the bytes to convert in a hex stringstart
- start index, inclusivelyend
- end index, exclusively- Returns:
- hex string representation of the byte array
-
byteToHexString
public static String byteToHexString(byte[] bytes)
Given an array of bytes it will convert the bytes to a hex string representation of the bytes.- Parameters:
bytes
- the bytes to convert in a hex string- Returns:
- hex string representation of the byte array
-
hexStringToByte
public static byte[] hexStringToByte(String hex)
Given a hex string this will return the byte array corresponding to the string .- Parameters:
hex
- the hex String array- Returns:
- a byte array that is a hex string representation of the given string. The size of the byte array is therefore hex.length/2
-
arrayAwareToString
public static String arrayAwareToString(Object o)
Converts the given object into a string representation by callingObject.toString()
and formatting (possibly nested) arrays andnull
.See
Arrays.deepToString(Object[])
for more information about the used format.
-
showControlCharacters
public static String showControlCharacters(String str)
Replaces control characters by their escape-coded version. For example, if the string contains a line break character ('\n'), this character will be replaced by the two characters backslash '\' and 'n'. As a consequence, the resulting string will not contain any more control characters.- Parameters:
str
- The string in which to replace the control characters.- Returns:
- The string with the replaced characters.
-
getRandomString
public static String getRandomString(Random rnd, int minLength, int maxLength)
Creates a random string with a length within the given interval. The string contains only characters that can be represented as a single code point.- Parameters:
rnd
- The random used to create the strings.minLength
- The minimum string length.maxLength
- The maximum string length (inclusive).- Returns:
- A random String.
-
getRandomString
public static String getRandomString(Random rnd, int minLength, int maxLength, char minValue, char maxValue)
Creates a random string with a length within the given interval. The string contains only characters that can be represented as a single code point.- Parameters:
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.- Returns:
- A random String.
-
generateRandomAlphanumericString
public static String generateRandomAlphanumericString(Random rnd, int length)
Creates a random alphanumeric string of given length.- Parameters:
rnd
- The random number generator to use.length
- The number of alphanumeric characters to append.
-
writeString
public static void writeString(@Nonnull String str, DataOutputView out) throws IOException
Writes a String to the given output. The written string can be read withreadString(DataInputView)
.- Parameters:
str
- The string to writeout
- The output to write to- Throws:
IOException
- Thrown, if the writing or the serialization fails.
-
readString
public static String readString(DataInputView in) throws IOException
Reads a non-null String from the given input.- Parameters:
in
- The input to read from- Returns:
- The deserialized String
- Throws:
IOException
- Thrown, if the reading or the deserialization fails.
-
writeNullableString
public static void writeNullableString(@Nullable String str, DataOutputView out) throws IOException
Writes a String to the given output. The string may be null. The written string can be read withreadNullableString(DataInputView)
-- Parameters:
str
- The string to write, or null.out
- The output to write to.- Throws:
IOException
- Thrown, if the writing or the serialization fails.
-
readNullableString
@Nullable public static String readNullableString(DataInputView in) throws IOException
Reads a String from the given input. The string may be null and must have been written withwriteNullableString(String, DataOutputView)
.- Parameters:
in
- The input to read from.- Returns:
- The deserialized string, or null.
- Throws:
IOException
- Thrown, if the reading or the deserialization fails.
-
isNullOrWhitespaceOnly
public static boolean isNullOrWhitespaceOnly(String str)
Checks if the string is null, empty, or contains only whitespace characters. A whitespace character is defined viaCharacter.isWhitespace(char)
.- Parameters:
str
- The string to check- Returns:
- True, if the string is null or blank, false otherwise.
-
concatenateWithAnd
@Nullable public static String concatenateWithAnd(@Nullable String s1, @Nullable String s2)
If both string arguments are non-null, this method concatenates them with ' and '. If only one of the arguments is non-null, this method returns the non-null argument. If both arguments are null, this method returns null.- Parameters:
s1
- The first string arguments2
- The second string argument- Returns:
- The concatenated string, or non-null argument, or null
-
toQuotedListString
public static String toQuotedListString(Object[] values)
Generates a string containing a comma-separated list of values in double-quotes. Uses lower-cased values returned fromObject.toString()
method for each element in the given array. Null values are skipped.- Parameters:
values
- array of elements for the list- Returns:
- The string with quoted list of elements
-
-