public class Strings extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
Strings.Result
The result of
substring(java.lang.String, int, char). |
| Modifier and Type | Field and Description |
|---|---|
static String |
EMPTY |
static String |
ESCAPE_JAVASCRIPT
Used with
escape(java.lang.String, java.lang.String) to escape a string in
JavaScript. |
| Constructor and Description |
|---|
Strings() |
| Modifier and Type | Method and Description |
|---|---|
static int |
anyOf(String src,
String delimiters,
int from)
Returns the index that is one of delimiters, or the length if none
of delimiter is found.
|
static String |
encode(int val)
Returns an encoded string, faster and shorter than
Long.toHexString.
|
static String |
encode(long val)
Returns an encoded string, faster and shorter than
Long.toHexString.
|
static StringBuffer |
encode(StringBuffer sb,
int val)
Returns an encoded string buffer, faster and shorter than
Integer.toHexString.
|
static StringBuffer |
encode(StringBuffer sb,
long val)
Returns an encoded string buffer, faster and shorter than
Long.toHexString.
|
static StringBuffer |
escape(StringBuffer dst,
CharSequence src,
String specials)
Escapes (a.k.a.
|
static StringBuilder |
escape(StringBuilder dst,
CharSequence src,
String specials)
Escapes (a.k.a.
|
static String |
escape(String src,
String specials)
Escapes (a.k.a, quote) the special characters with backslash.
|
static String |
escapeJavaScript(String text)
Escapes (a.k.a.
|
static int |
indexOf(StringBuffer sb,
char cc,
int j)
Returns the index of the give character in the given string buffer,
or -1 if not found.
|
static boolean |
isBlank(String s)
Returns true if the string is null or empty or pure blank.
|
static boolean |
isEmpty(String s)
Returns true if the string is null or empty.
|
static <T> String |
join(T[] array)
Joins the elements of the array into a single String separated by a comma.
|
static <T> String |
join(T[] array,
char separator)
Joins the elements of the array into a single String separated by the given separator.
|
static <T> String |
join(T[] array,
char separator,
int startIndex,
int endIndex)
Joins the elements of the array into a single String separated by the given separator
from the startIndex to the endIndex.
|
static int |
lastAnyOf(String src,
String delimiters,
int from)
The backward version of
anyOf(java.lang.String, java.lang.String, int). |
static int |
lastIndexOf(StringBuffer sb,
char cc,
int j)
Returns the last index of the give character in the given string buffer,
or -1 if not found.
|
static int |
nextSeparator(String src,
int from,
char[] separators,
boolean escBackslash,
boolean escQuot,
boolean quotAsSeparator)
Returns the next separator index in the src string.
|
static int |
nextSeparator(String src,
int from,
char[] separators,
boolean escBackslash,
boolean escQuot,
boolean quotAsSeparator,
boolean parenthesis)
Returns the next separator index in the src string.
|
static Strings.Result |
nextToken(String src,
int from,
char[] separators)
Returns the next token with unescape.
|
static Strings.Result |
nextToken(String src,
int from,
char[] separators,
boolean escBackslash,
boolean quotAsToken)
Returns the next token with additional options.
|
static Strings.Result |
nextToken(String src,
int from,
char[] separators,
boolean escBackslash,
boolean quotAsToken,
boolean parenthesis)
Returns the next token with additional options.
|
static int |
nextWhitespace(CharSequence src,
int from)
Returns the next whitespace.
|
static int |
skipWhitespaces(CharSequence src,
int from)
Returns the next index after skipping whitespaces.
|
static int |
skipWhitespacesBackward(CharSequence src,
int from)
The backward version of
skipWhitespaces(java.lang.CharSequence, int). |
static Strings.Result |
substring(String src,
int from,
char until)
Returns the substring from the
from index up to the
until character or end-of-string. |
static Strings.Result |
substring(String src,
int from,
char until,
boolean escBackslash)
Returns the substring from the
from index up to the
until character or end-of-string. |
static String |
toString(byte[] data)
Converts the given byte[] to UTF-8 string, if possible.
|
static StringBuffer |
trim(StringBuffer buf,
int index)
Trims the string buffer by removing the leading and trailing
whitespaces.
|
static String |
unescape(String s)
Un-escape the quoted string.
|
public static final String ESCAPE_JAVASCRIPT
escape(java.lang.String, java.lang.String) to escape a string in
JavaScript. It assumes the string will be enclosed with a single quote.public static final String EMPTY
public static final boolean isEmpty(String s)
public static final boolean isBlank(String s)
public static final StringBuffer trim(StringBuffer buf, int index)
Like String.trim(), a whitespace is a character that has
a code not great than '\u0020' (the space character)
buf - the string buffer to trim the leading and trailing.index - the index of the first character to trim
(i.e., consider as the leading character).
If 0, it starts from the beginning.public static final StringBuffer encode(StringBuffer sb, int val)
It works even in system that is case-insensitive, such as IE.
It is useful to generate a string to represent a number.
public static final StringBuffer encode(StringBuffer sb, long val)
It works even in system that is case-insensitive, such as IE.
It is useful to generate a string to represent a number.
public static final String encode(int val)
public static final String encode(long val)
public static final int indexOf(StringBuffer sb, char cc, int j)
sb.indexOf(""+cc, j);, but faster.public static final int lastIndexOf(StringBuffer sb, char cc, int j)
sb.lastIndexOf(""+cc, j);, but faster.public static final int anyOf(String src, String delimiters, int from)
Unlike String.indexOf(String, int), this method returns the first occurrence of any character in the delimiters.
This method is optimized to use String.indexOf(char, int) if it found the length of delimiter is 1.
src - the source string to searchfrom - the index to start the search fromdelimiters - the set of characters to search forlastAnyOf(java.lang.String, java.lang.String, int)public static final int lastAnyOf(String src, String delimiters, int from)
anyOf(java.lang.String, java.lang.String, int).
This method is optimized to use String.indexOf(char, int) if it found the length of delimiter is 1.
fromanyOf(java.lang.String, java.lang.String, int)public static final int skipWhitespaces(CharSequence src, int from)
public static final int skipWhitespacesBackward(CharSequence src, int from)
skipWhitespaces(java.lang.CharSequence, int).public static final int nextWhitespace(CharSequence src, int from)
public static final String escape(String src, String specials)
Note: specials usually contains '\\'.
For example, Maps.parse(java.util.Map<? super java.lang.String, ? super java.lang.String>, java.lang.String, char, char) will un-quote
backspace. Thus, if you want to preserve backslash, you have
invoke escape(s, "\\") before calling Maps.parse().
src - the string to process. If null, null is returned.specials - a string of characters that shall be escaped/quoted
To escape a string in JavaScript code snippet, you can use ESCAPE_JAVASCRIPT.unescape(java.lang.String)public static final StringBuffer escape(StringBuffer dst, CharSequence src, String specials)
dst - the destination buffer to append to.src - the source to escape from.specials - a string of characters that shall be escaped/quoted
To escape a string in JavaScript code snippet, you can use ESCAPE_JAVASCRIPT.public static final StringBuilder escape(StringBuilder dst, CharSequence src, String specials)
dst - the destination buffer to append to.src - the source to escape from.specials - a string of characters that shall be escaped/quoted
To escape a string in JavaScript code snippet, you can use ESCAPE_JAVASCRIPT.public static final String escapeJavaScript(String text)
Note: this implementation is referred from unbescape
public static final Strings.Result substring(String src, int from, char until)
from index up to the
until character or end-of-string.
Unlike String.subsring, it converts \f, \n, \t and \r. It doesn't
handle u and x yet.until character if found, or
a number larger than length() if no such character.public static final Strings.Result substring(String src, int from, char until, boolean escBackslash)
from index up to the
until character or end-of-string.escBackslash - whether to treat '\\' specially (as escape char)
It doesn't handle u and x yet.until character if found, or
a number larger than length() if no such character.
You can tell which case it is by examining Strings.Result.separator.public static final Strings.Result nextToken(String src, int from, char[] separators) throws IllegalSyntaxException
Strings.Result.separator.IllegalSyntaxException - if the quoted string is unclosed.public static final Strings.Result nextToken(String src, int from, char[] separators, boolean escBackslash, boolean quotAsToken) throws IllegalSyntaxException
nextToken(String, int, char[], boolean, boolean, boolean)
for more information.IllegalSyntaxException - if the quoted string is unclosed.nextToken(String, int, char[], boolean, boolean, boolean)public static final Strings.Result nextToken(String src, int from, char[] separators, boolean escBackslash, boolean quotAsToken, boolean parenthesis) throws IllegalSyntaxException
Strings.Result.separator.escBackslash - whether to treat '\\' specially (as escape char)
It doesn't handle u and x yet.quotAsToken - whether to treat characters inside '\'' or '"'
as a token. Note: the quotes are excluded from the token.parenthesis - whether to ignore separators and quotes inside
a pair of parentheses. Recognized parentheses include
{}, [] or ().IllegalSyntaxException - if the quoted string is unclosed.public static int nextSeparator(String src, int from, char[] separators, boolean escBackslash, boolean escQuot, boolean quotAsSeparator)
escQuot - whether to escape characters inside quotations
('\'' or '"'). In other words, ignore separators inside quotationsquotAsSeparator - whether to consider quotations as one of
the separatorspublic static int nextSeparator(String src, int from, char[] separators, boolean escBackslash, boolean escQuot, boolean quotAsSeparator, boolean parenthesis)
escQuot - whether to escape characters inside quotations
('\'' or '"'). In other words, it specifies whether to ignore
separators inside quotationsquotAsSeparator - whether to consider quotations as one of
the separators.
If escQuot is true, quotAsSeparator is ignored.parenthesis - whether to ignore separators and quotes in side
a pair of parentheses. Recognized parentheses include
{}, [] or ().public static final <T> String join(T[] array)
array - public static final <T> String join(T[] array, char separator)
array - separator - public static final <T> String join(T[] array, char separator, int startIndex, int endIndex)
array - separator - startIndex - endIndex - public static final String toString(byte[] data)
This is a method helper for JDK 5
Copyright © 2018. All rights reserved.