public class StringUtil extends Object
Modifier and Type | Field | Description |
---|---|---|
static String |
EMPTY_STRING |
Returns
Object.toString() of the provided value if it isn't null; "" otherwise. |
Constructor | Description |
---|---|
StringUtil() |
Modifier and Type | Method | Description |
---|---|---|
static String |
asEmptyIfNull(Object string) |
|
static String |
assertCharactersNotInString(String illegalChars,
char... chars) |
Checks that a String doesn't contain one or more characters of interest.
|
static String |
bytesToHexString(byte[] data) |
Convert a byte array into a String hex representation.
|
static String |
bytesToString(byte[] data) |
|
static String |
bytesToString(byte[] buffer,
int offset,
int length) |
|
static char |
byteToChar(byte b) |
Convert ASCII byte to ASCII char.
|
static void |
charsToBytes(char[] chars,
int charOffset,
int length,
byte[] bytes,
int byteOffset) |
Convert chars to bytes merely by casting
|
static byte |
charToByte(char c) |
Convert ASCII char to byte.
|
static int |
fromHexDigit(char c) |
|
static int |
hammingDistance(String s1,
String s2) |
Calculates the Hamming distance (number of character mismatches) between two strings s1 and s2.
|
static byte[] |
hexStringToBytes(String s) |
Convert a String containing hex characters into an array of bytes with the binary representation
of the hex string
|
static String |
intValuesToString(int[] intVals) |
|
static String |
intValuesToString(short[] shortVals) |
|
static boolean |
isBlank(String str) |
Checks if a String is whitespace, empty ("") or null.
|
static boolean |
isWithinHammingDistance(String s1,
String s2,
int maxHammingDistance) |
Determines if two strings s1 and s2 are within maxHammingDistance of each other using the Hamming distance metric.
|
static <T> String |
join(String separator,
Collection<T> objs) |
|
static <T> String |
join(String separator,
T... objs) |
|
static int |
levenshteinDistance(String string1,
String string2,
int swap,
int substitution,
int insertion,
int deletion) |
|
static String |
readNullTerminatedString(BinaryCodec binaryCodec) |
|
static String |
repeatCharNTimes(char c,
int repeatNumber) |
|
static String |
reverseString(String s) |
Reverse the given string.
|
static int |
split(String aString,
String[] tokens,
char delim) |
Split the string into tokens separated by the given delimiter.
|
static int |
splitConcatenateExcessTokens(String aString,
String[] tokens,
char delim) |
Split the string into tokens separated by the given delimiter.
|
static byte[] |
stringToBytes(String s) |
|
static byte[] |
stringToBytes(String s,
int offset,
int length) |
|
static char |
toHexDigit(int value) |
|
static byte |
toLowerCase(byte b) |
|
static byte |
toUpperCase(byte b) |
|
static void |
toUpperCase(byte[] bytes) |
Converts in place all lower case letters to upper case in the byte array provided.
|
static String |
wordWrap(String s,
int maxLineLength) |
Return input string with newlines inserted to ensure that all lines
have length <= maxLineLength.
|
static String |
wordWrapSingleLine(String s,
int maxLineLength) |
public static final String EMPTY_STRING
Object.toString()
of the provided value if it isn't null; "" otherwise.public static <T> String join(String separator, Collection<T> objs)
separator
- String to interject between each string in strings argobjs
- List of objs to be joinedpublic static int split(String aString, String[] tokens, char delim)
aString
- the string to splittokens
- an array to hold the parsed tokensdelim
- character that delimits tokenspublic static int splitConcatenateExcessTokens(String aString, String[] tokens, char delim)
aString
- the string to splittokens
- an array to hold the parsed tokensdelim
- character that delimits tokenspublic static byte toLowerCase(byte b)
b
- ASCII characterpublic static byte toUpperCase(byte b)
b
- ASCII characterpublic static void toUpperCase(byte[] bytes)
public static String assertCharactersNotInString(String illegalChars, char... chars)
illegalChars
- the String to checkchars
- the characters to check forIllegalArgumentException
- if the String contains one or more of the characterspublic static String wordWrap(String s, int maxLineLength)
public static String intValuesToString(int[] intVals)
public static String intValuesToString(short[] shortVals)
public static String bytesToString(byte[] data)
public static String bytesToString(byte[] buffer, int offset, int length)
public static byte[] stringToBytes(String s)
public static byte[] stringToBytes(String s, int offset, int length)
public static String readNullTerminatedString(BinaryCodec binaryCodec)
public static void charsToBytes(char[] chars, int charOffset, int length, byte[] bytes, int byteOffset)
chars
- input charscharOffset
- where to start converting from chars arraylength
- how many chars to convertbytes
- where to put the converted outputbyteOffset
- where to start writing the converted output.public static byte charToByte(char c)
public static char byteToChar(byte b)
public static String bytesToHexString(byte[] data)
data
- Input to be converted.public static byte[] hexStringToBytes(String s) throws NumberFormatException
s
- Hex string. Length must be even because each pair of hex chars is converted into a byte.NumberFormatException
public static char toHexDigit(int value)
public static int fromHexDigit(char c) throws NumberFormatException
NumberFormatException
public static String reverseString(String s)
s
- String to be reversed.public 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("sam") = false StringUtils.isBlank(" sam ") = false
str
- the String to check, may be nulltrue
if the String is null, empty or whitespacepublic static String repeatCharNTimes(char c, int repeatNumber)
public static int levenshteinDistance(String string1, String string2, int swap, int substitution, int insertion, int deletion)
public static int hammingDistance(String s1, String s2)
s1
- The first string to compares2
- The second string to compare, note that if s1 and s2 are swapped the value returned will be identical.IllegalArgumentException
- If the two strings have differing lengths.public static boolean isWithinHammingDistance(String s1, String s2, int maxHammingDistance)
s1
- The first string to compares2
- The second string to compare, note that if s1 and s2 are swapped the value returned will be identical.maxHammingDistance
- The largest Hamming distance the strings can have for this function to return true.IllegalArgumentException
- If the two strings have differing lengths.