public class StringUtils extends Object
This class is immutable and thread-safe.
CharEncoding
,
Standard charsetsConstructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
equals(CharSequence cs1,
CharSequence cs2)
Compares two CharSequences, returning
true if they represent equal sequences of characters. |
static ByteBuffer |
getByteBufferUtf8(String string)
Encodes the given string into a byte buffer using the UTF-8 charset, storing the result into a new byte
array.
|
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.
|
static String |
newString(byte[] bytes,
String charsetName)
Constructs a new
String by decoding the specified array of bytes using the given charset. |
static String |
newStringIso8859_1(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the ISO-8859-1 charset. |
static String |
newStringUsAscii(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the US-ASCII charset. |
static String |
newStringUtf16(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the UTF-16 charset. |
static String |
newStringUtf16Be(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the UTF-16BE charset. |
static String |
newStringUtf16Le(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the UTF-16LE charset. |
static String |
newStringUtf8(byte[] bytes)
Constructs a new
String by decoding the specified array of bytes using the UTF-8 charset. |
public static boolean equals(CharSequence cs1, CharSequence cs2)
Compares two CharSequences, returning true
if they represent equal sequences of characters.
null
s 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
Copied from Apache Commons Lang r1583482 on April 10, 2014 (day of 3.3.2 release).
cs1
- the first CharSequence, may be null
cs2
- the second CharSequence, may be null
true
if the CharSequences are equal (case-sensitive), or both null
Object.equals(Object)
public static ByteBuffer getByteBufferUtf8(String string)
string
- the String to encode, may be null
null
if the input string was null
NullPointerException
- Thrown if StandardCharsets.UTF_8
is not initialized, which should never happen since it is
required by the Java platform specification.getBytesUnchecked(String, String)
public static byte[] getBytesIso8859_1(String string)
string
- the String to encode, may be null
null
if the input string was null
NullPointerException
- Thrown if StandardCharsets.ISO_8859_1
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptiongetBytesUnchecked(String, String)
public static byte[] getBytesUnchecked(String string, String charsetName)
This method catches UnsupportedEncodingException
and rethrows it as IllegalStateException
, which
should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
string
- the String to encode, may be null
charsetName
- The name of a required Charset
null
if the input string was null
IllegalStateException
- Thrown when a UnsupportedEncodingException
is caught, which should never happen for a
required charset name.CharEncoding
,
String.getBytes(String)
public static byte[] getBytesUsAscii(String string)
string
- the String to encode, may be null
null
if the input string was null
NullPointerException
- Thrown if StandardCharsets.US_ASCII
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptiongetBytesUnchecked(String, String)
public static byte[] getBytesUtf16(String string)
string
- the String to encode, may be null
null
if the input string was null
NullPointerException
- Thrown if StandardCharsets.UTF_16
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptiongetBytesUnchecked(String, String)
public static byte[] getBytesUtf16Be(String string)
string
- the String to encode, may be null
null
if the input string was null
NullPointerException
- Thrown if StandardCharsets.UTF_16BE
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptiongetBytesUnchecked(String, String)
public static byte[] getBytesUtf16Le(String string)
string
- the String to encode, may be null
null
if the input string was null
NullPointerException
- Thrown if StandardCharsets.UTF_16LE
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptiongetBytesUnchecked(String, String)
public static byte[] getBytesUtf8(String string)
string
- the String to encode, may be null
null
if the input string was null
NullPointerException
- Thrown if StandardCharsets.UTF_8
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptiongetBytesUnchecked(String, String)
public static String newString(byte[] bytes, String charsetName)
String
by decoding the specified array of bytes using the given charset.
This method catches UnsupportedEncodingException
and re-throws it as IllegalStateException
, which
should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
bytes
- The bytes to be decoded into characters, may be null
charsetName
- The name of a required Charset
String
decoded from the specified array of bytes using the given charset,
or null
if the input byte array was null
.IllegalStateException
- Thrown when a UnsupportedEncodingException
is caught, which should never happen for a
required charset name.CharEncoding
,
String.String(byte[], String)
public static String newStringIso8859_1(byte[] bytes)
String
by decoding the specified array of bytes using the ISO-8859-1 charset.bytes
- The bytes to be decoded into characters, may be null
String
decoded from the specified array of bytes using the ISO-8859-1 charset, or
null
if the input byte array was null
.NullPointerException
- Thrown if StandardCharsets.ISO_8859_1
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptionpublic static String newStringUsAscii(byte[] bytes)
String
by decoding the specified array of bytes using the US-ASCII charset.bytes
- The bytes to be decoded into charactersString
decoded from the specified array of bytes using the US-ASCII charset,
or null
if the input byte array was null
.NullPointerException
- Thrown if StandardCharsets.US_ASCII
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptionpublic static String newStringUtf16(byte[] bytes)
String
by decoding the specified array of bytes using the UTF-16 charset.bytes
- The bytes to be decoded into charactersString
decoded from the specified array of bytes using the UTF-16 charset
or null
if the input byte array was null
.NullPointerException
- Thrown if StandardCharsets.UTF_16
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptionpublic static String newStringUtf16Be(byte[] bytes)
String
by decoding the specified array of bytes using the UTF-16BE charset.bytes
- The bytes to be decoded into charactersString
decoded from the specified array of bytes using the UTF-16BE charset,
or null
if the input byte array was null
.NullPointerException
- Thrown if StandardCharsets.UTF_16BE
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptionpublic static String newStringUtf16Le(byte[] bytes)
String
by decoding the specified array of bytes using the UTF-16LE charset.bytes
- The bytes to be decoded into charactersString
decoded from the specified array of bytes using the UTF-16LE charset,
or null
if the input byte array was null
.NullPointerException
- Thrown if StandardCharsets.UTF_16LE
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptionpublic static String newStringUtf8(byte[] bytes)
String
by decoding the specified array of bytes using the UTF-8 charset.bytes
- The bytes to be decoded into charactersString
decoded from the specified array of bytes using the UTF-8 charset,
or null
if the input byte array was null
.NullPointerException
- Thrown if StandardCharsets.UTF_8
is not initialized, which should never happen since it is
required by the Java platform specification.NullPointerException
instead of UnsupportedEncodingExceptionCopyright © 2002–2020 The Apache Software Foundation. All rights reserved.