Class StringUtils
This class is immutable and thread-safe.
- Since:
- 1.4
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequals(CharSequence cs1, CharSequence cs2) Compares two CharSequences, returningtrueif they represent equal sequences of characters.private static ByteBuffergetByteBuffer(String string, Charset charset) CallsString.getBytes(Charset)static ByteBuffergetByteBufferUtf8(String string) Encodes the given string into a byte buffer using the UTF-8 charset, storing the result into a new byte array.private static byte[]CallsString.getBytes(Charset)static byte[]getBytesIso8859_1(String string) Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new byte array.static byte[]getBytesUnchecked(String string, String charsetName) Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte array.static byte[]getBytesUsAscii(String string) Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte array.static byte[]getBytesUtf16(String string) Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte array.static byte[]getBytesUtf16Be(String string) Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte array.static byte[]getBytesUtf16Le(String string) Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte array.static byte[]getBytesUtf8(String string) Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.private static IllegalStateExceptionnewIllegalStateException(String charsetName, UnsupportedEncodingException e) static StringConstructs a newStringby decoding the specified array of bytes using the given charset.private static StringConstructs a newStringby decoding the specified array of bytes using the given charset.static StringnewStringIso8859_1(byte[] bytes) Constructs a newStringby decoding the specified array of bytes using the ISO-8859-1 charset.static StringnewStringUsAscii(byte[] bytes) Constructs a newStringby decoding the specified array of bytes using the US-ASCII charset.static StringnewStringUtf16(byte[] bytes) Constructs a newStringby decoding the specified array of bytes using the UTF-16 charset.static StringnewStringUtf16Be(byte[] bytes) Constructs a newStringby decoding the specified array of bytes using the UTF-16BE charset.static StringnewStringUtf16Le(byte[] bytes) Constructs a newStringby decoding the specified array of bytes using the UTF-16LE charset.static StringnewStringUtf8(byte[] bytes) Constructs a newStringby decoding the specified array of bytes using the UTF-8 charset.
-
Constructor Details
-
StringUtils
public StringUtils()
-
-
Method Details
-
equals
Compares two CharSequences, returning
trueif they represent equal sequences of characters.nulls are handled without exceptions. Twonullreferences 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") = falseCopied from Apache Commons Lang r1583482 on April 10, 2014 (day of 3.3.2 release).
- Parameters:
cs1- the first CharSequence, may benullcs2- the second CharSequence, may benull- Returns:
trueif the CharSequences are equal (case-sensitive), or bothnull- Since:
- 1.10
- See Also:
-
getByteBuffer
CallsString.getBytes(Charset)- Parameters:
string- The string to encode (if null, return null).charset- TheCharsetto encode theString- Returns:
- the encoded bytes
-
getByteBufferUtf8
Encodes the given string into a byte buffer using the UTF-8 charset, storing the result into a new byte array.- Parameters:
string- the String to encode, may benull- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_8is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- 1.11
- See Also:
-
getBytes
CallsString.getBytes(Charset)- Parameters:
string- The string to encode (if null, return null).charset- TheCharsetto encode theString- Returns:
- the encoded bytes
-
getBytesIso8859_1
Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new byte array.- Parameters:
string- the String to encode, may benull- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
NullPointerException- Thrown ifStandardCharsets.ISO_8859_1is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException - See Also:
-
getBytesUnchecked
Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte array.This method catches
UnsupportedEncodingExceptionand rethrows it asIllegalStateException, which should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.- Parameters:
string- the String to encode, may benullcharsetName- The name of a requiredCharset- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
IllegalStateException- Thrown when aUnsupportedEncodingExceptionis caught, which should never happen for a required charset name.- See Also:
-
getBytesUsAscii
Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte array.- Parameters:
string- the String to encode, may benull- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
NullPointerException- Thrown ifStandardCharsets.US_ASCIIis not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException - See Also:
-
getBytesUtf16
Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte array.- Parameters:
string- the String to encode, may benull- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_16is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException - See Also:
-
getBytesUtf16Be
Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte array.- Parameters:
string- the String to encode, may benull- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_16BEis not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException - See Also:
-
getBytesUtf16Le
Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte array.- Parameters:
string- the String to encode, may benull- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_16LEis not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException - See Also:
-
getBytesUtf8
Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte array.- Parameters:
string- the String to encode, may benull- Returns:
- encoded bytes, or
nullif the input string wasnull - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_8is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException - See Also:
-
newIllegalStateException
private static IllegalStateException newIllegalStateException(String charsetName, UnsupportedEncodingException e) -
newString
Constructs a newStringby decoding the specified array of bytes using the given charset.- Parameters:
bytes- The bytes to be decoded into characterscharset- TheCharsetto encode theString; notnull- Returns:
- A new
Stringdecoded from the specified array of bytes using the given charset, ornullif the input byte array wasnull. - Throws:
NullPointerException- Thrown if charset isnull
-
newString
Constructs a newStringby decoding the specified array of bytes using the given charset.This method catches
UnsupportedEncodingExceptionand re-throws it asIllegalStateException, which should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.- Parameters:
bytes- The bytes to be decoded into characters, may benullcharsetName- The name of a requiredCharset- Returns:
- A new
Stringdecoded from the specified array of bytes using the given charset, ornullif the input byte array wasnull. - Throws:
IllegalStateException- Thrown when aUnsupportedEncodingExceptionis caught, which should never happen for a required charset name.- See Also:
-
newStringIso8859_1
Constructs a newStringby decoding the specified array of bytes using the ISO-8859-1 charset.- Parameters:
bytes- The bytes to be decoded into characters, may benull- Returns:
- A new
Stringdecoded from the specified array of bytes using the ISO-8859-1 charset, ornullif the input byte array wasnull. - Throws:
NullPointerException- Thrown ifStandardCharsets.ISO_8859_1is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException
-
newStringUsAscii
Constructs a newStringby decoding the specified array of bytes using the US-ASCII charset.- Parameters:
bytes- The bytes to be decoded into characters- Returns:
- A new
Stringdecoded from the specified array of bytes using the US-ASCII charset, ornullif the input byte array wasnull. - Throws:
NullPointerException- Thrown ifStandardCharsets.US_ASCIIis not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException
-
newStringUtf16
Constructs a newStringby decoding the specified array of bytes using the UTF-16 charset.- Parameters:
bytes- The bytes to be decoded into characters- Returns:
- A new
Stringdecoded from the specified array of bytes using the UTF-16 charset ornullif the input byte array wasnull. - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_16is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException
-
newStringUtf16Be
Constructs a newStringby decoding the specified array of bytes using the UTF-16BE charset.- Parameters:
bytes- The bytes to be decoded into characters- Returns:
- A new
Stringdecoded from the specified array of bytes using the UTF-16BE charset, ornullif the input byte array wasnull. - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_16BEis not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException
-
newStringUtf16Le
Constructs a newStringby decoding the specified array of bytes using the UTF-16LE charset.- Parameters:
bytes- The bytes to be decoded into characters- Returns:
- A new
Stringdecoded from the specified array of bytes using the UTF-16LE charset, ornullif the input byte array wasnull. - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_16LEis not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException
-
newStringUtf8
Constructs a newStringby decoding the specified array of bytes using the UTF-8 charset.- Parameters:
bytes- The bytes to be decoded into characters- Returns:
- A new
Stringdecoded from the specified array of bytes using the UTF-8 charset, ornullif the input byte array wasnull. - Throws:
NullPointerException- Thrown ifStandardCharsets.UTF_8is not initialized, which should never happen since it is required by the Java platform specification.- Since:
- As of 1.7, throws
NullPointerExceptioninstead of UnsupportedEncodingException
-