public final class Strings extends Object
| Modifier and Type | Field and Description |
|---|---|
static byte[] |
EMPTY_BYTES
An empty byte array
|
static String |
EMPTY_STRING
An empty String
|
static String[] |
EMPTY_STRING_ARRAY
An empty String array
|
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
The ASCI chars
|
| 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,
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,
String text)
Check if a text is present at the current position in a buffer.
|
static int |
areEquals(char[] chars,
int index,
String text,
boolean caseSensitive)
Check if a text is present at the current position in a buffer.
|
static boolean |
areEquals(String string,
int index,
String text)
Check if a text is present at the current position in another string.
|
static String |
asciiBytesToString(byte[] bytes)
Transform an array of ASCII bytes to a string. the byte array should contains
only values in [0, 127].
|
static byte |
byteAt(byte[] bytes,
int index)
Get the byte at a given position in a byte array, checking for limits
|
static String |
byteToString(byte b)
Helper function that dump a byte as a double digit value
|
static String |
centerTrunc(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(String string,
int index)
Get the character at a given position in a string, checking for limits
|
static int |
compare(byte[] b1,
byte[] b2) |
static byte[] |
copy(byte[] value)
Copy a byte array into a new byte array
|
static String |
deepTrim(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 String |
deepTrim(String str,
boolean toLowerCase)
Put common code to deepTrim(String) and deepTrimToLower here.
|
static String |
deepTrimToLower(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 String |
dumpByte(byte octet)
Helper function that dump a byte in hex form
|
static 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 String |
dumpHexPairs(byte[] buffer)
Helper function that dump an array of bytes in hex pair form,
without '0x' and space chars
|
static boolean |
equals(String str1,
String str2)
Compares two Strings, returning
true if they are equal. |
static String |
formatHtml(String source,
boolean replaceNl,
boolean replaceTag,
boolean replaceQuote)
This method is used to insert HTML block dynamically
|
static byte[] |
getBytesUtf8(String string)
Return UTF-8 encoded byte[] representation of a String
|
static byte[] |
getBytesUtf8Ascii(String string)
When the string to convert to bytes is pure ascii, this is a faster
method than the getBytesUtf8.
|
static String |
getDefaultCharsetName()
Get the default charset
|
static String |
getString(byte[] data,
Charset charset)
From commons-httpclients.
|
static String |
getString(byte[] data,
int offset,
int length,
Charset charset)
From commons-httpclients.
|
static String |
getString(byte[] data,
int offset,
int length,
String charset)
From commons-httpclients.
|
static String |
getString(byte[] data,
String charset)
From commons-httpclients.
|
static 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(char[] charArray,
int index,
char car)
Test if the current character is equal to a specific character.
|
static boolean |
isCharASCII(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(String str)
Checks if a String is empty ("") or null.
|
static boolean |
isIA5String(String str)
Check that a String is a valid IA5String.
|
static boolean |
isNotEmpty(String str)
Checks if a String is not empty ("") and not null.
|
static boolean |
isPrintableString(String str)
Check that a String is a valid PrintableString.
|
static boolean |
isValidUuid(String uuid)
Checks to see if a String is a valid UUID.
|
static String |
listToString(List<?> list)
Utility method that return a String representation of a list
|
static String |
listToString(List<?> list,
String tabs)
Utility method that return a String representation of a list
|
static String |
lowerCase(String str)
Converts a String to lower case as per
String.toLowerCase(). |
static String |
lowerCaseAscii(String str)
Rewrote the toLowercase method to improve performances.
|
static String |
mapToString(Map<?,?> map)
Utility method that return a String representation of a map.
|
static String |
mapToString(Map<?,?> map,
String tabs)
Utility method that return a String representation of a map.
|
static int |
parseInt(String value)
Past an ASCII String to a number
|
static String |
setToString(Set<?> set)
Utility method that return a String representation of a set
|
static byte[] |
toByteArray(String hexString)
Get byte array from hex string
|
static String |
toHexString(byte[] res)
Gets a hex string from byte array.
|
static String |
toLowerCase(byte[] value)
Rewrote the toLowercase method to improve performances.
|
static String |
toLowerCase(String value)
Deprecated.
|
static String |
toLowerCaseAscii(String value)
Rewrote the toLowercase method to improve performances.
|
static String |
toUpperCase(String value)
Deprecated.
|
static String |
toUpperCaseAscii(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 String |
trim(String str)
Removes spaces (char <= 32) from both start and ends of this String,
handling
null by returning null. |
static String |
trimConsecutiveToOne(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 String |
trimLeft(String str)
Removes spaces (char <= 32) from start of this String, handling
null by returning null. |
static void |
trimLeft(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 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 String |
trimRight(String str)
Removes spaces (char <= 32) from end of this String, handling
null by returning null. |
static String |
trimRight(String str,
int escapedSpace)
Removes spaces (char <= 32) from end of this String, handling
null by returning null. |
static String |
trimRight(String string,
Position pos)
Removes spaces (char <= 32) from end of this string, handling
null by returning null. |
static String |
upperCase(String str)
Converts a String to upper case as per
String.toUpperCase( Locale ). |
static String |
utf8ToString(byte[] bytes)
Return an UTF-8 encoded String
|
static String |
utf8ToString(byte[] bytes,
int length)
Return an UTF-8 encoded String
|
static String |
utf8ToString(byte[] bytes,
int start,
int length)
Return an UTF-8 encoded String
|
static byte[] |
uuidToBytes(String string)
converts the string representation of an UUID to bytes
|
static String |
uuidToString(byte[] bytes)
converts the bytes of a UUID to string
|
private static final org.slf4j.Logger LOG
public 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
private static final byte[] UTF8
public static final byte[] EMPTY_BYTES
public static final String EMPTY_STRING
public static final String[] EMPTY_STRING_ARRAY
public static String dumpBytes(byte[] buffer)
buffer - The bytes array to dumppublic static String byteToString(byte b)
b - The byte to dumppublic static String dumpByte(byte octet)
octet - The byte to dumppublic static char dumpHex(byte hex)
hex - The hex to dumppublic static String dumpHexPairs(byte[] buffer)
buffer - The bytes array to dumppublic static String deepTrim(String str, boolean toLowerCase)
str - the string to deep trimtoLowerCase - how to normalize for case: upper or lowerdeepTrim( String )public static String deepTrimToLower(String string)
string - The String to modifydeepTrim( String )public static String deepTrim(String string)
string - the string to deep trim.public static String trimConsecutiveToOne(String str, char ch)
str - the string to trim consecutive characters ofch - the character to trim downpublic static String centerTrunc(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 String toHexString(byte[] res)
res - the byte arraypublic static byte[] toByteArray(String hexString)
hexString - the hex string to convert to a byte arraypublic static String formatHtml(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(String string, int index, 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(char[] charArray,
int index,
char car)
charArray - 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(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 String utf8ToString(byte[] bytes)
bytes - The byte array to be transformed to a Stringpublic static 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 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,
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,
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,
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(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 String trim(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 String trimLeft(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 nullpos - The position in the char[]public static void trimLeft(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 position in the Stringpublic 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 position in the byte[]public 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 nullpos - The position in the byte[]public static String trimRight(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 String trimRight(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 nullpos - The position in the char[]public static String trimRight(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 nullpos - The position in the Stringpublic static 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 nullpos - The position in the byte[]public 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 nullpos - The position in the byte[]public static char charAt(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 String asciiBytesToString(byte[] bytes)
bytes - The byte array to transformpublic static byte[] getBytesUtf8(String string)
string - The string to be transformed to a byte arraypublic static byte[] getBytesUtf8Ascii(String string)
string - The string to convert to byte[]public static String getDefaultCharsetName()
public static boolean equals(String str1, 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 String listToString(List<?> list)
list - The list to transform to a stringpublic static String setToString(Set<?> set)
set - The set to transform to a stringpublic static String listToString(List<?> list, String tabs)
list - The list to transform to a stringtabs - The tabs to add in front of the elementspublic static String mapToString(Map<?,?> map)
map - The map to transform to a stringpublic static String mapToString(Map<?,?> map, String tabs)
map - The map to transform to a stringtabs - The tabs to add in ffront of the elements@Deprecated public static String toLowerCase(String value)
toLowerCaseAscii(String)toLowerCaseAscii(String)value - The String to lowercasepublic static String toLowerCaseAscii(String value)
value - The String to lowercasepublic static String toLowerCase(byte[] value)
value - The byte[] to lowercase@Deprecated public static String toUpperCase(String value)
toUpperCaseAscii(String)toUpperCaseAscii(String)value - The String to uppercasepublic static String toUpperCaseAscii(String value)
value - The String to uppercasepublic static String upperCase(String str)
Converts a String to upper case as per String.toUpperCase( Locale ).
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 String lowerCase(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 String lowerCaseAscii(String str)
str - The String to lowercasepublic static boolean isPrintableString(String str)
str - The String to checktrue if the string is a PrintableString or is empty,
false otherwisepublic static boolean isNotEmpty(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(String str)
str - The String to checktrue if the string is an IA5String or is empty,
false otherwisepublic static boolean isValidUuid(String uuid)
uuid - the UUID to check for validitypublic static String uuidToString(byte[] bytes)
bytes - bytes of a UUIDpublic static byte[] uuidToBytes(String string)
string - the string representation of an UUIDpublic static byte[] copy(byte[] value)
value - the byte array to copypublic static String getString(byte[] data, int offset, int length, 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 String getString(byte[] data, int offset, int length, Charset 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 String getString(byte[] data, String charset)
data - the byte array to be encodedcharset - the desired character encodingpublic static String getString(byte[] data, Charset charset)
data - the byte array to be encodedcharset - the desired character encodingpublic static String getUUID(long value)
value - The least significant bits.public static int parseInt(String value)
value - The string to parsepublic static int compare(byte[] b1,
byte[] b2)
b1 - The first byte[] to compareb2 - The second byte[] to compareComparator.compare(java.lang.Object, java.lang.Object)Copyright © 2003–2023 The Apache Software Foundation. All rights reserved.