public abstract class Strings
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
DefaultBufferSize |
Constructor and Description |
---|
Strings() |
Modifier and Type | Method and Description |
---|---|
static boolean |
asBool(java.lang.String boolString) |
static boolean |
asBool(java.lang.String boolString,
boolean defaultIfEmpty)
Attempts to convert a string representing a conceptual "true/false" into a boolean.
|
static java.lang.String |
escapeHTML(java.lang.String s) |
static java.lang.String |
escapeStringLiteral(java.lang.String subject)
Used to convert raw characters to their escaped version
when these raw version cannot be used as part of a Java
string literal.
|
static java.lang.String |
escapeStringLiteral(java.lang.String subject,
int escapeOptions)
Used to convert raw characters to their escaped version
when these raw version cannot be used as part of a Java
string literal.
|
static java.lang.String |
firstNonEmpty(java.lang.String... names)
Returns the first string in the list of parameters that is not null or empty ("")
Returns empty ("") if all strings are either null or empty.
|
static java.lang.String |
fmt(java.lang.String format,
java.lang.Object... args) |
static java.lang.String |
fromChar(char fillWith,
int length)
Creates a string of the specified length, composed by repeating the specified character.
|
static boolean |
isEmpty(java.lang.String str)
Returns true if the string is null or empty ("").
|
static boolean |
isWhiteSpace(java.lang.String str)
Returns true if the string is null, empty or is composed entirely of
whitespace (as determined by Character.is
(e.g.
|
static <T> java.lang.String |
join(java.lang.String delim,
java.lang.Iterable<T> values) |
static <T> java.lang.String |
join(java.lang.String delimiter,
java.lang.Iterable<T> values,
java.util.function.Function<? super T,java.lang.String> formatter) |
static <T> java.lang.String |
join(java.lang.String delim,
T[] values) |
static java.lang.String |
md5(java.lang.String input)
Returns a 32-character hexadecimal-formatted MD5 hash of the subject string.
|
static java.lang.String |
pad(int targetLength,
java.lang.String subject) |
static java.lang.String |
pad(int targetLength,
java.lang.String subject,
char padWith,
int truncate)
Pad a string with the specified character.
|
static java.lang.String |
pad0s(int targetLength,
java.lang.String subject) |
static java.lang.String |
padl(int targetLength,
java.lang.String subject)
Pad-Left.
|
static java.lang.String |
padr(int targetLength,
java.lang.String subject)
Pad-Right.
|
private static java.text.ParseException |
parseError(java.lang.String message,
int pos) |
static java.lang.String |
readAll(java.io.InputStream input) |
static java.lang.String |
readAll(java.io.InputStream input,
java.nio.charset.Charset charset) |
static java.lang.String |
readAll(java.io.InputStream input,
java.nio.charset.Charset charset,
int bufferSize) |
static java.lang.String |
readAll(java.io.InputStream input,
java.lang.String charset) |
static java.lang.String |
readAll(java.io.InputStream input,
java.lang.String charset,
int bufferSize) |
static java.lang.String |
replaceNonPrintable(java.lang.String s,
char replaceControlChars,
char replaceWhiteSpace) |
static java.lang.String |
replaceNonPrintableU(java.lang.String s,
char replaceControlChars,
char replaceWhiteSpace)
Same as
replaceNonPrintable(java.lang.String, char, char) but includes support for unicode characters, including Surrogate pairs. |
static java.lang.String |
toFriendlyName(java.lang.String name)
Converts a camel-case name into space separated words.
|
static java.lang.String |
unescapeStringLiteral(java.lang.String subject)
UN-escapes a string by replacing backslash-escaped sequences with
the character literals they represent.
|
static void |
writeAll(java.lang.String content,
java.io.OutputStream output) |
static void |
writeAll(java.lang.String content,
java.io.OutputStream output,
java.nio.charset.Charset charset) |
static void |
writeAll(java.lang.String content,
java.io.OutputStream output,
java.nio.charset.Charset charset,
int bufferSize) |
static void |
writeAll(java.lang.String content,
java.io.OutputStream output,
java.lang.String charset) |
static void |
writeAll(java.lang.String content,
java.io.OutputStream output,
java.lang.String charset,
int bufferSize) |
public static final int DefaultBufferSize
public static boolean asBool(java.lang.String boolString)
public static boolean asBool(java.lang.String boolString, boolean defaultIfEmpty)
defaultIfEmpty
if boolString
is null, empty (""), or whitespace.
Returns FALSE if boolString
== "false", "no" or "0" (case insensitive, trimmed).
True otherwise.public static java.lang.String escapeHTML(java.lang.String s)
public static java.lang.String escapeStringLiteral(java.lang.String subject)
Escapes NULL, BKSP, TAB, CR, LF, FF, backslash, double (") and single (') quotes to their java escape codes. Also escapes all other control characters (1 to 31) and all characters above 127 to the corresponding Java unicode escape code (, ⚽ etc. )
subject
- The string to escape.escapeStringLiteral(String,int)
public static java.lang.String escapeStringLiteral(java.lang.String subject, int escapeOptions)
Escapes NULL, BKSP, TAB, CR, LF, FF, backslash, double (") and single (') quotes to their java escape codes. Also escapes all other control characters (1 to 31) and all characters above 127 to the corresponding Java unicode escape code (, ⚽ etc. )
subject
- The string to escape.escapeOptions
- A bitfield of CharType
values indicating which types
of characters to escape.
However note CharType.CONTROL
characters are ALWAYS escaped, while
CharType.ALPHA
, CharType.DIGITS
, and most CharType.SYMBOLS
are NEVER escaped.
The only character types for which escaping can be enabled or disabled are the following:
CharType.TAB
, CharType.NEWLINE
, CharType.DOUBLE_QUOTES
,
CharType.SINGLE_QUOTE
, CharType.BACKSLASH
, and CharType.EXT_ASCII
.
escapeStringLiteral(String, int)
@NotNull public static java.lang.String firstNonEmpty(java.lang.String... names)
public static java.lang.String fmt(java.lang.String format, java.lang.Object... args)
public static java.lang.String fromChar(char fillWith, int length)
fillWith
- The character used to fill the string.length
- The desired length of the string.public static boolean isEmpty(java.lang.String str)
str
- public static boolean isWhiteSpace(@Nullable java.lang.String str)
str
- public static <T> java.lang.String join(java.lang.String delim, java.lang.Iterable<T> values)
public static <T> java.lang.String join(java.lang.String delimiter, java.lang.Iterable<T> values, @Nullable java.util.function.Function<? super T,java.lang.String> formatter)
public static <T> java.lang.String join(java.lang.String delim, T[] values)
public static java.lang.String md5(java.lang.String input)
public static java.lang.String pad(int targetLength, java.lang.String subject)
public static java.lang.String pad(int targetLength, java.lang.String subject, char padWith, int truncate)
targetLength
- Determines both the side to pad on and the target number of characters to return.
A negative value causes padding to be placed on the left side, while a positive one pads on the right.
Padding is added until the string length is at least abs(width) long. (Where abs is the absolute value function).subject
- The string to pad.padWith
- The character to pad with.truncate
- An int that specifies whether the string should be truncated if its current length is larger than the desired width.
0 indicates that no truncating should occur. If the string is longer than width, it is returned unmodified.
1 indicates that the string should be truncated by removing excess characters from the RIGHT side of the string.
-1 indicates that the string should be truncated by removing excess characters from the LEFT side of the string.public static java.lang.String pad0s(int targetLength, java.lang.String subject)
public static java.lang.String padl(int targetLength, java.lang.String subject)
public static java.lang.String padr(int targetLength, java.lang.String subject)
private static java.text.ParseException parseError(java.lang.String message, int pos)
public static java.lang.String readAll(java.io.InputStream input) throws java.io.IOException
java.io.IOException
public static java.lang.String readAll(java.io.InputStream input, java.nio.charset.Charset charset) throws java.io.IOException
java.io.IOException
public static java.lang.String readAll(java.io.InputStream input, java.nio.charset.Charset charset, int bufferSize) throws java.io.IOException
java.io.IOException
public static java.lang.String readAll(java.io.InputStream input, java.lang.String charset) throws java.io.IOException
java.io.IOException
public static java.lang.String readAll(java.io.InputStream input, java.lang.String charset, int bufferSize) throws java.io.IOException
java.io.IOException
public static java.lang.String replaceNonPrintable(java.lang.String s, char replaceControlChars, char replaceWhiteSpace)
public static java.lang.String replaceNonPrintableU(java.lang.String s, char replaceControlChars, char replaceWhiteSpace)
replaceNonPrintable(java.lang.String, char, char)
but includes support for unicode characters, including Surrogate pairs.public static java.lang.String toFriendlyName(java.lang.String name)
@NotNull public static java.lang.String unescapeStringLiteral(@NotNull java.lang.String subject) throws java.text.ParseException
\\ \" \'
are replaced by backslash (\), double-quote ("), and apostrophe (') respectively.
\r \n \t \f \b \a
are replaced by CR, LF, TAB, FormFeed, and BackSpace respectively.
\e \a
are replaced by ESC and ALERT respectively
\cX
is replaced by the control character represented by X (e.g. \cM is the carriage return (CR, 0x0D, #13)
\O \OO \OOO
are replaced by their octal character values (where 'O' can be any digit 0-7)
\xHH
is replaced by its hexadecimal character value (where 'H' is a hex digit: 0-9, A-F, or a-f)
\\uHHHH
is replaced by its unicode character value ('H' as above).
\UHHHHHHHH
is replaced by its unicode character value ('H' as above).
Unknown backslash escape sequences are passed through as-is. They do not result in errors.
E.g. "c:\Windows" yields "c:\Windows" whereas "c:\Users" yields ERROR (invalid \U escape)
Similarly, a trailing backslash is not considered an error and is simply passed through. "hi\" yields "hi\"subject
- java.text.ParseException
- if any of the \c \O \x \\u or \U
escapes is NOT followed by a valid
sequence of valid characters of the appropriate length.public static void writeAll(java.lang.String content, java.io.OutputStream output) throws java.io.IOException
java.io.IOException
public static void writeAll(java.lang.String content, java.io.OutputStream output, java.nio.charset.Charset charset) throws java.io.IOException
java.io.IOException
public static void writeAll(java.lang.String content, java.io.OutputStream output, java.nio.charset.Charset charset, int bufferSize) throws java.io.IOException
java.io.IOException
public static void writeAll(java.lang.String content, java.io.OutputStream output, java.lang.String charset) throws java.io.IOException
java.io.IOException
public static void writeAll(java.lang.String content, java.io.OutputStream output, java.lang.String charset, int bufferSize) throws java.io.IOException
java.io.IOException