public final class Strings
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static byte[] |
EMPTY_BYTES
An empty byte array
|
static java.lang.String |
EMPTY_STRING
An empty String
|
private static byte[] |
HEX_CHAR
Hex chars
|
private static boolean[] |
IS_PRINTABLE_CHAR
A table containing booleans when the corresponding char is printable
|
private static org.slf4j.Logger |
LOG
A logger for this class
|
private static char[] |
TO_LOWER_CASE |
private static byte[] |
TO_LOWER_CASE_BYTE |
private static char[] |
UPPER_CASE
upperCase = 'A' ..
|
private static byte[] |
UTF8 |
| Modifier | Constructor and Description |
|---|---|
private |
Strings()
Private constructor
|
| Modifier and Type | Method and Description |
|---|---|
static int |
areEquals(byte[] bytes,
int index,
byte[] bytes2)
Check if a text is present at the current position in a buffer.
|
static int |
areEquals(byte[] bytes,
int index,
java.lang.String text)
Check if a text is present at the current position in a buffer.
|
static int |
areEquals(char[] chars,
int index,
char[] chars2)
Check if a text is present at the current position in a buffer.
|
static int |
areEquals(char[] chars,
int index,
char[] chars2,
boolean caseSensitive)
Check if a text is present at the current position in a buffer.
|
static int |
areEquals(char[] chars,
int index,
java.lang.String text)
Check if a text is present at the current position in a buffer.
|
static int |
areEquals(char[] chars,
int index,
java.lang.String text,
boolean caseSensitive)
Check if a text is present at the current position in a buffer.
|
static boolean |
areEquals(java.lang.String string,
int index,
java.lang.String text)
Check if a text is present at the current position in another string.
|
static java.lang.String |
asciiBytesToString(byte[] bytes)
Transform an array of ASCII bytes to a string.
|
static byte |
byteAt(byte[] bytes,
int index)
Get the byte at a given position in a byte array, checking for limits
|
static java.lang.String |
centerTrunc(java.lang.String str,
int head,
int tail)
Truncates large Strings showing a portion of the String's head and tail
with the center cut out and replaced with '...'.
|
static char |
charAt(char[] chars,
int index)
Get the char at a given position in a byte array, checking for limits
|
static char |
charAt(java.lang.String string,
int index)
Get the character at a given position in a string, checking for limits
|
static byte[] |
copy(byte[] value)
Copy a byte array into a new byte array
|
static java.lang.String |
deepTrim(java.lang.String string)
A deep trim of a string remove whitespace from the ends as well as
excessive whitespace within the inside of the string between
non-whitespace characters.
|
static java.lang.String |
deepTrim(java.lang.String str,
boolean toLowerCase)
Put common code to deepTrim(String) and deepTrimToLower here.
|
static java.lang.String |
deepTrimToLower(java.lang.String string)
This does the same thing as a trim but we also lowercase the string while
performing the deep trim within the same buffer.
|
static java.lang.String |
dumpByte(byte octet)
Helper function that dump a byte in hex form
|
static java.lang.String |
dumpBytes(byte[] buffer)
Helper function that dump an array of bytes in hex form
|
static char |
dumpHex(byte hex)
Helper function that returns a char from an hex
|
static java.lang.String |
dumpHexPairs(byte[] buffer)
Helper function that dump an array of bytes in hex pair form,
without '0x' and space chars
|
static boolean |
equals(java.lang.String str1,
java.lang.String str2)
Compares two Strings, returning
true if they are equal. |
static java.lang.String |
formatHtml(java.lang.String source,
boolean replaceNl,
boolean replaceTag,
boolean replaceQuote)
This method is used to insert HTML block dynamically
|
static byte[] |
getBytesUtf8(java.lang.String string)
Return UTF-8 encoded byte[] representation of a String
|
static byte[] |
getBytesUtf8Ascii(java.lang.String string)
When the string to convert to bytes is pure ascii, this is a faster
method than the getBytesUtf8.
|
static java.lang.String |
getDefaultCharsetName()
Get the default charset
|
static java.lang.String |
getString(byte[] data,
int offset,
int length,
java.lang.String charset)
From commons-httpclients.
|
static java.lang.String |
getString(byte[] data,
java.lang.String charset)
From commons-httpclients.
|
static java.lang.String |
getUUID(long value)
Create a new UUID using a long as the least significant bits
|
static boolean |
isCharASCII(byte[] byteArray,
int index,
char car)
Test if the current character is equal to a specific character.
|
static boolean |
isCharASCII(java.lang.String string,
int index,
char car)
Test if the current character is equal to a specific character.
|
static boolean |
isEmpty(byte[] bytes)
Checks if a bytes array is empty or null.
|
static boolean |
isEmpty(java.lang.String str)
Checks if a String is empty ("") or null.
|
static boolean |
isIA5String(java.lang.String str)
Check that a String is a valid IA5String.
|
static boolean |
isNotEmpty(java.lang.String str)
Checks if a String is not empty ("") and not null.
|
static boolean |
isPrintableString(java.lang.String str)
Check that a String is a valid PrintableString.
|
static boolean |
isValidUuid(java.lang.String uuid)
Checks to see if a String is a valid UUID.
|
static java.lang.String |
listToString(java.util.List<?> list)
Utility method that return a String representation of a list
|
static java.lang.String |
listToString(java.util.List<?> list,
java.lang.String tabs)
Utility method that return a String representation of a list
|
static java.lang.String |
lowerCase(java.lang.String str)
Converts a String to lower case as per
String.toLowerCase(). |
static java.lang.String |
lowerCaseAscii(java.lang.String str)
Rewrote the toLowercase method to improve performances.
|
static java.lang.String |
mapToString(java.util.Map<?,?> map)
Utility method that return a String representation of a map.
|
static java.lang.String |
mapToString(java.util.Map<?,?> map,
java.lang.String tabs)
Utility method that return a String representation of a map.
|
static int |
parseInt(java.lang.String value)
Past an ASCII String to a number
|
static java.lang.String |
setToString(java.util.Set<?> set)
Utility method that return a String representation of a set
|
static byte[] |
toByteArray(java.lang.String hexString)
Get byte array from hex string
|
static java.lang.String |
toHexString(byte[] res)
Gets a hex string from byte array.
|
static java.lang.String |
toLowerCase(byte[] value)
Rewrote the toLowercase method to improve performances.
|
static java.lang.String |
toLowerCase(java.lang.String value)
Deprecated.
|
static java.lang.String |
toLowerCaseAscii(java.lang.String value)
Rewrote the toLowercase method to improve performances.
|
static java.lang.String |
toUpperCase(java.lang.String value)
Deprecated.
|
static java.lang.String |
toUpperCaseAscii(java.lang.String value)
Rewrote the toLowercase method to improve performances.
|
static byte[] |
trim(byte[] bytes)
Removes spaces (char <= 32) from both start and ends of this bytes
array, handling
null by returning null. |
static java.lang.String |
trim(java.lang.String str)
Removes spaces (char <= 32) from both start and ends of this String,
handling
null by returning null. |
static java.lang.String |
trimConsecutiveToOne(java.lang.String str,
char ch)
Trims several consecutive characters into one.
|
static int |
trimLeft(byte[] bytes,
int pos)
Removes spaces (char <= 32) from start of this array, handling
null by returning null. |
static void |
trimLeft(byte[] bytes,
Position pos)
Removes spaces (char <= 32) from a position in this array, handling
null by returning null. |
static int |
trimLeft(char[] chars,
int pos)
Removes spaces (char <= 32) from start of this array, handling
null by returning null. |
static java.lang.String |
trimLeft(java.lang.String str)
Removes spaces (char <= 32) from start of this String, handling
null by returning null. |
static void |
trimLeft(java.lang.String string,
Position pos)
Removes spaces (char <= 32) from a position in this array, handling
null by returning null. |
static int |
trimRight(byte[] bytes,
int pos)
Removes spaces (char <= 32) from end of this array, handling
null by returning null. |
static java.lang.String |
trimRight(byte[] bytes,
Position pos)
Removes spaces (char <= 32) from end of this string, handling
null by returning null. |
static int |
trimRight(char[] chars,
int pos)
Removes spaces (char <= 32) from end of this array, handling
null by returning null. |
static java.lang.String |
trimRight(java.lang.String str)
Removes spaces (char <= 32) from end of this String, handling
null by returning null. |
static java.lang.String |
trimRight(java.lang.String str,
int escapedSpace)
Removes spaces (char <= 32) from end of this String, handling
null by returning null. |
static java.lang.String |
trimRight(java.lang.String string,
Position pos)
Removes spaces (char <= 32) from end of this string, handling
null by returning null. |
static java.lang.String |
upperCase(java.lang.String str)
Converts a String to upper case as per
String#toUpperCase( Locale.ROOT ). |
static java.lang.String |
utf8ToString(byte[] bytes)
Return an UTF-8 encoded String
|
static java.lang.String |
utf8ToString(byte[] bytes,
int length)
Return an UTF-8 encoded String
|
static java.lang.String |
utf8ToString(byte[] bytes,
int start,
int length)
Return an UTF-8 encoded String
|
static byte[] |
uuidToBytes(java.lang.String string)
converts the string representation of an UUID to bytes
|
static java.lang.String |
uuidToString(byte[] bytes)
converts the bytes of a UUID to string
|
private static final org.slf4j.Logger LOG
private static final byte[] HEX_CHAR
private static final boolean[] IS_PRINTABLE_CHAR
private static final char[] TO_LOWER_CASE
private static final byte[] TO_LOWER_CASE_BYTE
private static final char[] UPPER_CASE
public static final byte[] EMPTY_BYTES
public static final java.lang.String EMPTY_STRING
private static final byte[] UTF8
public static java.lang.String dumpBytes(byte[] buffer)
buffer - The bytes array to dumppublic static java.lang.String dumpByte(byte octet)
octet - The byte to dumppublic static char dumpHex(byte hex)
hex - The hex to dumppublic static java.lang.String dumpHexPairs(byte[] buffer)
buffer - The bytes array to dumppublic static java.lang.String deepTrim(java.lang.String str,
boolean toLowerCase)
str - the string to deep trimtoLowerCase - how to normalize for case: upper or lowerTODO Replace the toCharArray() by substring manipulationspublic static java.lang.String deepTrimToLower(java.lang.String string)
deepTrim( String )public static java.lang.String deepTrim(java.lang.String string)
string - the string to deep trim.public static java.lang.String trimConsecutiveToOne(java.lang.String str,
char ch)
str - the string to trim consecutive characters ofch - the character to trim downpublic static java.lang.String centerTrunc(java.lang.String str,
int head,
int tail)
str - the string to truncatehead - the amount of the head to displaytail - the amount of the tail to displaypublic static java.lang.String toHexString(byte[] res)
res - the byte arraypublic static byte[] toByteArray(java.lang.String hexString)
hexString - the hex string to convert to a byte arraypublic static java.lang.String formatHtml(java.lang.String source,
boolean replaceNl,
boolean replaceTag,
boolean replaceQuote)
source - the HTML code to be processesreplaceNl - if true '\n' will be replaced by <br>replaceTag - if true '<' will be replaced by < and '>' will be replaced
by >replaceQuote - if true '\"' will be replaced by "public static boolean areEquals(java.lang.String string,
int index,
java.lang.String text)
string - The string which contains the dataindex - Current position in the stringtext - The text we want to checktrue if the string contains the text.public static boolean isCharASCII(byte[] byteArray,
int index,
char car)
byteArray - The buffer which contains the dataindex - Current position in the buffercar - The character we want to compare with the current buffer positiontrue if the current character equals the given character.public static boolean isCharASCII(java.lang.String string,
int index,
char car)
string - The String which contains the dataindex - Current position in the stringcar - The character we want to compare with the current string positiontrue if the current character equals the given character.public static java.lang.String utf8ToString(byte[] bytes)
bytes - The byte array to be transformed to a Stringpublic static java.lang.String utf8ToString(byte[] bytes,
int length)
bytes - The byte array to be transformed to a Stringlength - The length of the byte array to be convertedpublic static java.lang.String utf8ToString(byte[] bytes,
int start,
int length)
bytes - The byte array to be transformed to a Stringstart - the starting position in the byte arraylength - The length of the byte array to be convertedpublic static int areEquals(byte[] bytes,
int index,
java.lang.String text)
bytes - The buffer which contains the dataindex - Current position in the buffertext - The text we want to checktrue if the buffer contains the text.public static int areEquals(char[] chars,
int index,
java.lang.String text)
chars - The buffer which contains the dataindex - Current position in the buffertext - The text we want to checktrue if the buffer contains the text.public static int areEquals(char[] chars,
int index,
java.lang.String text,
boolean caseSensitive)
chars - The buffer which contains the dataindex - Current position in the buffertext - The text we want to checkcaseSensitive - If the comparison is case-sensitivetrue if the buffer contains the text.public static int areEquals(char[] chars,
int index,
char[] chars2)
chars - The buffer which contains the dataindex - Current position in the bufferchars2 - The text we want to checktrue if the buffer contains the text.public static int areEquals(char[] chars,
int index,
char[] chars2,
boolean caseSensitive)
chars - The buffer which contains the dataindex - Current position in the bufferchars2 - The text we want to checkcaseSensitive - If the comparison is case-sensitivetrue if the buffer contains the text.public static int areEquals(byte[] bytes,
int index,
byte[] bytes2)
bytes - The buffer which contains the dataindex - Current position in the bufferbytes2 - The text we want to checktrue if the buffer contains the text.public static boolean isEmpty(java.lang.String str)
Checks if a String is empty ("") or null.
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 String. That functionality is available in isBlank().
str - the String to check, may be nulltrue if the String is empty or nullpublic static boolean isEmpty(byte[] bytes)
bytes - The bytes array to check, may be nulltrue if the bytes array is empty or nullpublic static java.lang.String trim(java.lang.String str)
Removes spaces (char <= 32) from both start and ends of this String,
handling null by returning null.
StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim("abc") = "abc"
StringUtils.trim(" abc ") = "abc"
str - the String to be trimmed, may be nullnull if null String inputpublic static byte[] trim(byte[] bytes)
Removes spaces (char <= 32) from both start and ends of this bytes
array, handling null by returning null.
StringUtils.trim(null) = null
StringUtils.trim("") = ""
StringUtils.trim(" ") = ""
StringUtils.trim("abc") = "abc"
StringUtils.trim(" abc ") = "abc"
bytes - the byte array to be trimmed, may be nullpublic static java.lang.String trimLeft(java.lang.String str)
Removes spaces (char <= 32) from start of this String, handling
null by returning null.
StringUtils.trimLeft(null) = null
StringUtils.trimLeft("") = ""
StringUtils.trimLeft(" ") = ""
StringUtils.trimLeft("abc") = "abc"
StringUtils.trimLeft(" abc ") = "abc "
str - the String to be trimmed, may be nullnull if null String inputpublic static int trimLeft(char[] chars,
int pos)
Removes spaces (char <= 32) from start of this array, handling
null by returning null.
StringUtils.trimLeft(null) = null
StringUtils.trimLeft("") = ""
StringUtils.trimLeft(" ") = ""
StringUtils.trimLeft("abc") = "abc"
StringUtils.trimLeft(" abc ") = "abc "
chars - the chars array to be trimmed, may be nullpublic static void trimLeft(java.lang.String string,
Position pos)
Removes spaces (char <= 32) from a position in this array, handling
null by returning null.
StringUtils.trimLeft(null) = null
StringUtils.trimLeft("",...) = ""
StringUtils.trimLeft(" ",...) = ""
StringUtils.trimLeft("abc",...) = "abc"
StringUtils.trimLeft(" abc ",...) = "abc "
string - the string to be trimmed, may be nullpos - The starting positionpublic static void trimLeft(byte[] bytes,
Position pos)
Removes spaces (char <= 32) from a position in this array, handling
null by returning null.
StringUtils.trimLeft(null) = null
StringUtils.trimLeft("",...) = ""
StringUtils.trimLeft(" ",...) = ""
StringUtils.trimLeft("abc",...) = "abc"
StringUtils.trimLeft(" abc ",...) = "abc "
bytes - the byte array to be trimmed, may be nullpos - The starting positionpublic static int trimLeft(byte[] bytes,
int pos)
Removes spaces (char <= 32) from start of this array, handling
null by returning null.
StringUtils.trimLeft(null) = null
StringUtils.trimLeft("") = ""
StringUtils.trimLeft(" ") = ""
StringUtils.trimLeft("abc") = "abc"
StringUtils.trimLeft(" abc ") = "abc "
bytes - the byte array to be trimmed, may be nullpublic static java.lang.String trimRight(java.lang.String str)
Removes spaces (char <= 32) from end of this String, handling
null by returning null.
StringUtils.trimRight(null) = null
StringUtils.trimRight("") = ""
StringUtils.trimRight(" ") = ""
StringUtils.trimRight("abc") = "abc"
StringUtils.trimRight(" abc ") = " abc"
str - the String to be trimmed, may be nullnull if null String inputpublic static java.lang.String trimRight(java.lang.String str,
int escapedSpace)
Removes spaces (char <= 32) from end of this String, handling
null by returning null.
StringUtils.trimRight(null) = null
StringUtils.trimRight("") = ""
StringUtils.trimRight(" ") = ""
StringUtils.trimRight("abc") = "abc"
StringUtils.trimRight(" abc ") = " abc"
str - the String to be trimmed, may be nullescapedSpace - The last escaped space, if anynull if null String inputpublic static int trimRight(char[] chars,
int pos)
Removes spaces (char <= 32) from end of this array, handling
null by returning null.
StringUtils.trimRight(null) = null
StringUtils.trimRight("") = ""
StringUtils.trimRight(" ") = ""
StringUtils.trimRight("abc") = "abc"
StringUtils.trimRight(" abc ") = " abc"
chars - the chars array to be trimmed, may be nullpublic static java.lang.String trimRight(java.lang.String string,
Position pos)
Removes spaces (char <= 32) from end of this string, handling
null by returning null.
StringUtils.trimRight(null) = null
StringUtils.trimRight("") = ""
StringUtils.trimRight(" ") = ""
StringUtils.trimRight("abc") = "abc"
StringUtils.trimRight(" abc ") = " abc"
string - the string to be trimmed, may be nullpublic static java.lang.String trimRight(byte[] bytes,
Position pos)
Removes spaces (char <= 32) from end of this string, handling
null by returning null.
StringUtils.trimRight(null) = null
StringUtils.trimRight("") = ""
StringUtils.trimRight(" ") = ""
StringUtils.trimRight("abc") = "abc"
StringUtils.trimRight(" abc ") = " abc"
bytes - the byte array to be trimmed, may be nullpublic static int trimRight(byte[] bytes,
int pos)
Removes spaces (char <= 32) from end of this array, handling
null by returning null.
StringUtils.trimRight(null) = null
StringUtils.trimRight("") = ""
StringUtils.trimRight(" ") = ""
StringUtils.trimRight("abc") = "abc"
StringUtils.trimRight(" abc ") = " abc"
bytes - the byte array to be trimmed, may be nullpublic static char charAt(java.lang.String string,
int index)
string - The string which contains the dataindex - Current position in the stringpublic static byte byteAt(byte[] bytes,
int index)
bytes - The byte[] which contains the dataindex - Current position in the byte[]public static char charAt(char[] chars,
int index)
chars - The char[] which contains the dataindex - Current position in the char[]public static java.lang.String asciiBytesToString(byte[] bytes)
bytes - The byte array to transformpublic static byte[] getBytesUtf8(java.lang.String string)
string - The string to be transformed to a byte arraypublic static byte[] getBytesUtf8Ascii(java.lang.String string)
string - The string to convert to byte[]public static java.lang.String getDefaultCharsetName()
public static boolean equals(java.lang.String str1,
java.lang.String str2)
Compares two Strings, returning true if they are equal.
nulls are handled without exceptions. Two
null references are considered to be equal. The comparison
is case sensitive.
StringUtils.equals(null, null) = true
StringUtils.equals(null, "abc") = false
StringUtils.equals("abc", null) = false
StringUtils.equals("abc", "abc") = true
StringUtils.equals("abc", "ABC") = false
str1 - the first String, may be nullstr2 - the second String, may be nulltrue if the Strings are equal, case sensitive, or
both nullString.equals(Object)public static java.lang.String listToString(java.util.List<?> list)
list - The list to transform to a stringpublic static java.lang.String setToString(java.util.Set<?> set)
set - The set to transform to a stringpublic static java.lang.String listToString(java.util.List<?> list,
java.lang.String tabs)
list - The list to transform to a stringtabs - The tabs to add in front of the elementspublic static java.lang.String mapToString(java.util.Map<?,?> map)
map - The map to transform to a stringpublic static java.lang.String mapToString(java.util.Map<?,?> map,
java.lang.String tabs)
map - The map to transform to a stringtabs - The tabs to add in ffront of the elementspublic static java.lang.String toLowerCase(java.lang.String value)
toLowerCaseAscii(String)value - The String to lowercasepublic static java.lang.String toLowerCaseAscii(java.lang.String value)
value - The String to lowercasepublic static java.lang.String toLowerCase(byte[] value)
value - The byte[] to lowercasepublic static java.lang.String toUpperCase(java.lang.String value)
toUpperCaseAscii(String)value - The String to uppercasepublic static java.lang.String toUpperCaseAscii(java.lang.String value)
value - The String to uppercasepublic static java.lang.String upperCase(java.lang.String str)
Converts a String to upper case as per String#toUpperCase( Locale.ROOT ).
A null input String returns null.
StringUtils.upperCase(null) = null
StringUtils.upperCase("") = ""
StringUtils.upperCase("aBc") = "ABC"
str - the String to upper case, may be nullnull if null String inputpublic static java.lang.String lowerCase(java.lang.String str)
Converts a String to lower case as per String.toLowerCase().
A null input String returns null.
StringUtils.lowerCase(null) = null
StringUtils.lowerCase("") = ""
StringUtils.lowerCase("aBc") = "abc"
str - the String to lower case, may be nullnull if null String inputpublic static java.lang.String lowerCaseAscii(java.lang.String str)
str - The String to lowercasepublic static boolean isPrintableString(java.lang.String str)
str - The String to checktrue if the string is a PrintableString or is empty,
false otherwisepublic static boolean isNotEmpty(java.lang.String str)
Checks if a String is not empty ("") and not null.
StringUtils.isNotEmpty(null) = false
StringUtils.isNotEmpty("") = false
StringUtils.isNotEmpty(" ") = true
StringUtils.isNotEmpty("bob") = true
StringUtils.isNotEmpty(" bob ") = true
str - the String to check, may be nulltrue if the String is not empty and not nullpublic static boolean isIA5String(java.lang.String str)
str - The String to checktrue if the string is an IA5String or is empty,
false otherwisepublic static boolean isValidUuid(java.lang.String uuid)
uuid - the UUID to check for validitypublic static java.lang.String uuidToString(byte[] bytes)
bytes - bytes of a UUIDpublic static byte[] uuidToBytes(java.lang.String string)
string - the string representation of an UUIDpublic static byte[] copy(byte[] value)
value - the byte array to copypublic static java.lang.String getString(byte[] data,
int offset,
int length,
java.lang.String charset)
data - the byte array to be encodedoffset - the index of the first byte to encodelength - the number of bytes to encodecharset - the desired character encodingpublic static java.lang.String getString(byte[] data,
java.lang.String charset)
data - the byte array to be encodedcharset - the desired character encodingpublic static java.lang.String getUUID(long value)
value - The least significant bits.public static int parseInt(java.lang.String value)
throws java.lang.NumberFormatException
value - The string to parsejava.lang.NumberFormatException - If we don't have a number