public class StringUtils extends Object
toUtf8(String) and fromUtf8(byte[]).| Modifier and Type | Field and Description |
|---|---|
static byte[] |
EMPTY_BYTES |
static Charset |
UTF8_CHARSET
Deprecated.
|
static String |
UTF8_STRING |
| Constructor and Description |
|---|
StringUtils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
chop(String s,
int maxBytes)
Returns the string truncated to maxBytes.
|
static int |
compareUnicode(String a,
String b)
Compares two Java Strings in Unicode code-point order.
|
static int |
compareUtf8(byte[] a,
byte[] b)
Compares two UTF-8 byte strings in Unicode code-point order.
|
static int |
compareUtf8UsingJavaStringOrdering(byte[] a,
byte[] b)
Compares two UTF-8 byte strings in UTF-16 code-unit order.
|
static int |
compareUtf8UsingJavaStringOrdering(ByteBuffer buf1,
int position1,
int length1,
ByteBuffer buf2,
int position2,
int length2)
Compares two UTF-8 byte strings in UTF-16 code-unit order.
|
static int |
compareUtf8UsingJavaStringOrdering(byte byte1,
byte byte2)
Compares two bytes from UTF-8 strings in such a way that the entire byte arrays are compared in UTF-16
code-unit order.
|
static byte[] |
decodeBase64(byte[] input)
Decode an input byte array using the
Base64 encoding scheme and return a newly-allocated byte array |
static byte[] |
decodeBase64String(String input)
Decode an input string using the
Base64 encoding scheme and return a newly-allocated byte array |
static String |
emptyToNullNonDruidDataString(String string)
Returns the given string if it is nonempty;
null otherwise. |
static byte[] |
encodeBase64(byte[] input)
Convert an input byte array into a newly-allocated byte array using the
Base64 encoding scheme |
static String |
encodeBase64String(byte[] input)
Convert an input byte array into a string using the
Base64 encoding scheme |
static String |
encodeForFormat(String s)
Encodes a string "s" for insertion into a format string.
|
static int |
estimatedBinaryLengthAsUTF8(String value) |
static String |
fastLooseChop(String s,
int maxBytes)
Shorten "s" to "maxBytes" chars.
|
static String |
format(String message,
Object... formatArgs)
Equivalent of String.format(Locale.ENGLISH, message, formatArgs).
|
static String |
fromUtf8(byte[] bytes) |
static String |
fromUtf8(byte[] bytes,
int offset,
int length) |
static String |
fromUtf8(it.unimi.dsi.fastutil.bytes.ByteArrayList buffer) |
static String |
fromUtf8(ByteBuffer buffer)
Decodes a UTF-8 string from the remaining bytes of a non-null buffer.
|
static String |
fromUtf8(ByteBuffer buffer,
int numBytes)
Decodes a UTF-8 String from
numBytes bytes starting at the current position of a buffer. |
static String |
fromUtf8Nullable(ByteBuffer buffer)
If buffer is Decodes a UTF-8 string from the remaining bytes of a buffer.
|
static String |
getResource(Object ref,
String resource) |
static String |
lpad(String base,
int len,
String pad)
Returns the string left-padded with the string pad to a length of len characters.
|
static String |
maybeRemoveLeadingSlash(String s) |
static String |
maybeRemoveTrailingSlash(String s) |
static String |
nonStrictFormat(String message,
Object... formatArgs)
Formats the string as
format(String, Object...), but instead of failing on illegal format, returns the
concatenated format string and format arguments. |
static String |
nullToEmptyNonDruidDataString(String string)
Returns the given string if it is non-null; the empty string otherwise.
|
static String |
removeChar(String s,
char c)
Removes all occurrences of the given char from the given string.
|
static String |
repeat(String s,
int count)
Returns a string whose value is the concatenation of the
string
s repeated count times. |
static String |
replace(String s,
String target,
String replacement)
Replaces all occurrences of the given target substring in the given string with the given replacement string.
|
static String |
replaceChar(String s,
char c,
String replacement)
Replaces all occurrences of the given char in the given string with the given replacement string.
|
static String |
rpad(String base,
int len,
String pad)
Returns the string right-padded with the string pad to a length of len characters.
|
static String |
toLowerCase(String s) |
static String |
toUpperCase(String s) |
static byte[] |
toUtf8(String string)
Converts a string to a UTF-8 byte array.
|
static ByteBuffer |
toUtf8ByteBuffer(String string)
Converts a string to UTF-8 bytes, returning them as a newly-allocated on-heap
ByteBuffer. |
static byte[] |
toUtf8Nullable(String string) |
static int |
toUtf8WithLimit(String string,
ByteBuffer byteBuffer)
Encodes "string" into the buffer "byteBuffer", using no more than the number of bytes remaining in the buffer.
|
static byte[] |
toUtf8WithNullToEmpty(String string) |
static String |
urlDecode(String s) |
static String |
urlEncode(String s)
Encodes a String in application/x-www-form-urlencoded format, with one exception:
"+" in the encoded form is replaced with "%20".
|
static String |
utf8Base64(String input)
Convert an input to base 64 and return the utf8 string of that byte array
|
public static final byte[] EMPTY_BYTES
@Deprecated public static final Charset UTF8_CHARSET
public static final String UTF8_STRING
public static int estimatedBinaryLengthAsUTF8(String value)
public static byte[] toUtf8WithNullToEmpty(String string)
public static int compareUnicode(String a, String b)
compareUtf8(byte[], byte[]), but is not consistent with
String.compareTo(String).public static int compareUtf8(byte[] a,
byte[] b)
compareUnicode(String, String), but is not consistent with
String.compareTo(String). For an ordering consistent with String.compareTo(String), use
compareUtf8UsingJavaStringOrdering(byte[], byte[]) instead.public static int compareUtf8UsingJavaStringOrdering(byte[] a,
byte[] b)
String.compareTo(String), but is not consistent with
compareUnicode(String, String) or compareUtf8(byte[], byte[]).public static int compareUtf8UsingJavaStringOrdering(ByteBuffer buf1, int position1, int length1, ByteBuffer buf2, int position2, int length2)
String.compareTo(String), but is not consistent with
compareUnicode(String, String) or compareUtf8(byte[], byte[]).public static int compareUtf8UsingJavaStringOrdering(byte byte1,
byte byte2)
compareUtf8UsingJavaStringOrdering(byte[], byte[]) and
compareUtf8UsingJavaStringOrdering(ByteBuffer, int, int, ByteBuffer, int, int).public static String fromUtf8(byte[] bytes)
public static String fromUtf8(byte[] bytes, int offset, int length)
public static String fromUtf8(ByteBuffer buffer, int numBytes)
numBytes bytes starting at the current position of a buffer.
Advances the position of the buffer by numBytes.public static String fromUtf8(ByteBuffer buffer)
Buffer.remaining().
Use fromUtf8Nullable(ByteBuffer) if the buffer might be null.public static String fromUtf8(it.unimi.dsi.fastutil.bytes.ByteArrayList buffer)
@Nullable public static String fromUtf8Nullable(@Nullable ByteBuffer buffer)
Buffer.remaining().
If the value is null, this method returns null. If the buffer will never be null, use fromUtf8(ByteBuffer)
instead.public static byte[] toUtf8(String string)
NullPointerException - if "string" is null@Nullable public static ByteBuffer toUtf8ByteBuffer(@Nullable String string)
ByteBuffer.
If "string" is null, returns null.public static int toUtf8WithLimit(String string, ByteBuffer byteBuffer)
public static String format(String message, Object... formatArgs)
public static String nonStrictFormat(String message, Object... formatArgs)
format(String, Object...), but instead of failing on illegal format, returns the
concatenated format string and format arguments. Should be used for unimportant formatting like logging,
exception messages, typically not directly.@Nullable public static String encodeForFormat(@Nullable String s)
@Nullable public static String urlEncode(@Nullable String s)
s - String to be encodedpublic static String removeChar(String s, char c)
s.replace("c", "").public static String replaceChar(String s, char c, String replacement)
s.replace("c", replacement).public static String replace(String s, String target, String replacement)
s.replace(target, replacement).public static String nullToEmptyNonDruidDataString(@Nullable String string)
string - the string to test and possibly returnstring itself if it is non-null; "" if it is null@Nullable public static String emptyToNullNonDruidDataString(@Nullable String string)
null otherwise.
This method should only be used at places where null to empty conversion is
irrelevant to null handling of the data.string - the string to test and possibly returnstring itself if it is nonempty; null if it is
empty or nullpublic static String utf8Base64(String input)
input - The string to convert to base64public static byte[] encodeBase64(byte[] input)
Base64 encoding schemeinput - The byte array to convert to base64public static String encodeBase64String(byte[] input)
Base64 encoding schemeinput - The byte array to convert to base64public static byte[] decodeBase64(byte[] input)
Base64 encoding scheme and return a newly-allocated byte arrayinput - The byte array to decode from base64public static byte[] decodeBase64String(String input)
Base64 encoding scheme and return a newly-allocated byte arrayinput - The string to decode from base64public static String repeat(String s, int count)
s repeated count times.
If count or length is zero then the empty string is returned.
This method may be used to create space padding for formatting text or zero padding for formatting numbers.
count - number of times to repeatcount times or the empty string if count
or length is zero.IllegalArgumentException - if the count is negative.@Nonnull public static String lpad(@Nonnull String base, int len, @Nonnull String pad)
base - The base string to be paddedlen - The length of padded stringpad - The pad string@Nonnull public static String rpad(@Nonnull String base, int len, @Nonnull String pad)
base - The base string to be paddedlen - The length of padded stringpad - The pad string@Nullable public static String chop(@Nullable String s, int maxBytes)
s - The input string to possibly be truncatedmaxBytes - The max bytes that string input will be truncated to@Nullable public static String fastLooseChop(@Nullable String s, int maxBytes)
chop(String, int) for slower, but accurate chopping.Copyright © 2011–2023 The Apache Software Foundation. All rights reserved.