Class Strings
- java.lang.Object
-
- org.zkoss.lang.Strings
-
public class Strings extends java.lang.ObjectString utilities and constants- Author:
- tomyeh
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStrings.ResultThe result ofsubstring(java.lang.String, int, char).
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringEMPTY
-
Constructor Summary
Constructors Constructor Description Strings()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intanyOf(java.lang.String src, java.lang.String delimiters, int from)Returns the index that is one of delimiters, or the length if none of delimiter is found.static java.lang.StringdefaultIfEmpty(java.lang.String value, java.lang.String defaultValue)Returns the passed value, or the default value if it is empty.static java.lang.Stringencode(int val)Returns an encoded string, faster and shorter than Long.toHexString.static java.lang.Stringencode(long val)Returns an encoded string, faster and shorter than Long.toHexString.static java.lang.StringBufferencode(java.lang.StringBuffer sb, int val)Returns an encoded string buffer, faster and shorter than Integer.toHexString.static java.lang.StringBufferencode(java.lang.StringBuffer sb, long val)Returns an encoded string buffer, faster and shorter than Long.toHexString.static java.lang.Stringescape(java.lang.String src, java.lang.String specials)Escapes (a.k.a, quote) the special characters with backslash.static intindexOf(java.lang.StringBuffer sb, char cc, int j)Returns the index of the give character in the given string buffer, or -1 if not found.static booleanisBlank(java.lang.String s)Returns true if the string is null or empty or pure blank.static booleanisEmpty(java.lang.String s)Returns true if the string is null or empty.static <T> java.lang.Stringjoin(T[] array)Joins the elements of the array into a single String separated by a comma.static <T> java.lang.Stringjoin(T[] array, char separator)Joins the elements of the array into a single String separated by the given separator.static <T> java.lang.Stringjoin(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 intlastAnyOf(java.lang.String src, java.lang.String delimiters, int from)The backward version ofanyOf(java.lang.String, java.lang.String, int).static intlastIndexOf(java.lang.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 intnextSeparator(java.lang.String src, int from, char[] separators, boolean escBackslash, boolean escQuot, boolean quotAsSeparator)Returns the next separator index in the src string.static intnextSeparator(java.lang.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.ResultnextToken(java.lang.String src, int from, char[] separators)Returns the next token with unescape.static Strings.ResultnextToken(java.lang.String src, int from, char[] separators, boolean escBackslash, boolean quotAsToken)Returns the next token with additional options.static Strings.ResultnextToken(java.lang.String src, int from, char[] separators, boolean escBackslash, boolean quotAsToken, boolean parenthesis)Returns the next token with additional options.static intnextWhitespace(java.lang.CharSequence src, int from)Returns the next whitespace.static intskipWhitespaces(java.lang.CharSequence src, int from)Returns the next index after skipping whitespaces.static intskipWhitespacesBackward(java.lang.CharSequence src, int from)The backward version ofskipWhitespaces(java.lang.CharSequence, int).static Strings.Resultsubstring(java.lang.String src, int from, char until)Returns the substring from thefromindex up to theuntilcharacter or end-of-string.static Strings.Resultsubstring(java.lang.String src, int from, char until, boolean escBackslash)Returns the substring from thefromindex up to theuntilcharacter or end-of-string.static java.lang.StringtoString(byte[] data)Converts the given byte[] to UTF-8 string, if possible.static java.lang.StringBuffertrim(java.lang.StringBuffer buf, int index)Trims the string buffer by removing the leading and trailing whitespaces.static java.lang.Stringunescape(java.lang.String s)Un-escape the quoted string.
-
-
-
Field Detail
-
EMPTY
public static final java.lang.String EMPTY
- See Also:
- Constant Field Values
-
-
Method Detail
-
isEmpty
public static final boolean isEmpty(java.lang.String s)
Returns true if the string is null or empty.
-
isBlank
public static final boolean isBlank(java.lang.String s)
Returns true if the string is null or empty or pure blank.
-
trim
public static final java.lang.StringBuffer trim(java.lang.StringBuffer buf, int index)Trims the string buffer by removing the leading and trailing whitespaces. Note: unlike String.trim(), you can specify the index of the first character to start trimming.Like String.trim(), a whitespace is a character that has a code not great than
'\u0020'(the space character)- Parameters:
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.- Returns:
- the same string buffer
- Since:
- 3.0.4
-
encode
public static final java.lang.StringBuffer encode(java.lang.StringBuffer sb, int val)Returns an encoded string buffer, faster and shorter than Integer.toHexString. It uses numbers and lower-case letters only. Thus it is a valid variable name if prefix with an alphabet. At least one character is generated.It works even in system that is case-insensitive, such as IE.
It is useful to generate a string to represent a number.
-
encode
public static final java.lang.StringBuffer encode(java.lang.StringBuffer sb, long val)Returns an encoded string buffer, faster and shorter than Long.toHexString. It uses numbers and lower-case letters only. Thus it is a valid variable name if prefix with an alphabet. At least one character is generated.It works even in system that is case-insensitive, such as IE.
It is useful to generate a string to represent a number.
-
encode
public static final java.lang.String encode(int val)
Returns an encoded string, faster and shorter than Long.toHexString.
-
encode
public static final java.lang.String encode(long val)
Returns an encoded string, faster and shorter than Long.toHexString.
-
indexOf
public static final int indexOf(java.lang.StringBuffer sb, char cc, int j)Returns the index of the give character in the given string buffer, or -1 if not found. It is equivalent tosb.indexOf(""+cc, j);, but faster.- Since:
- 5.0.3
-
lastIndexOf
public static final int lastIndexOf(java.lang.StringBuffer sb, char cc, int j)Returns the last index of the give character in the given string buffer, or -1 if not found. It is equivalent tosb.lastIndexOf(""+cc, j);, but faster.- Since:
- 5.0.3
-
anyOf
public static final int anyOf(java.lang.String src, java.lang.String delimiters, int from)Returns the index that is one of delimiters, or the length if none of delimiter is found.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.
- Parameters:
src- the source string to searchfrom- the index to start the search fromdelimiters- the set of characters to search for- Returns:
- the index that is one of delimiters. If return >= src.length(), it means no such delimiters
- See Also:
lastAnyOf(java.lang.String, java.lang.String, int)
-
lastAnyOf
public static final int lastAnyOf(java.lang.String src, java.lang.String delimiters, int from)The backward version ofanyOf(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.
- Returns:
- the previous index that is one of delimiter.
If it is negative, it means no delimiter in front of
from - See Also:
anyOf(java.lang.String, java.lang.String, int)
-
skipWhitespaces
public static final int skipWhitespaces(java.lang.CharSequence src, int from)Returns the next index after skipping whitespaces.
-
skipWhitespacesBackward
public static final int skipWhitespacesBackward(java.lang.CharSequence src, int from)The backward version ofskipWhitespaces(java.lang.CharSequence, int).- Returns:
- the next index that is not a whitespace. If it is negative, it means no whitespace in front of it.
-
nextWhitespace
public static final int nextWhitespace(java.lang.CharSequence src, int from)Returns the next whitespace.
-
escape
public static final java.lang.String escape(java.lang.String src, java.lang.String specials)Escapes (a.k.a, quote) the special characters with backslash. It prefix a backslash to any characters specified in the specials argument.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().- Parameters:
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 useEncode.forJavaScript(String).- See Also:
unescape(java.lang.String)
-
unescape
public static final java.lang.String unescape(java.lang.String s)
Un-escape the quoted string.
-
substring
public static final Strings.Result substring(java.lang.String src, int from, char until)
Returns the substring from thefromindex up to theuntilcharacter or end-of-string. Unlike String.subsring, it converts \f, \n, \t and \r. It doesn't handle u and x yet.- Returns:
- the result (never null). Result.next is the position of
the
untilcharacter if found, or a number larger than length() if no such character.
-
substring
public static final Strings.Result substring(java.lang.String src, int from, char until, boolean escBackslash)
Returns the substring from thefromindex up to theuntilcharacter or end-of-string.- Parameters:
escBackslash- whether to treat '\\' specially (as escape char) It doesn't handle u and x yet.- Returns:
- the result (never null). Result.next is the position of
the
untilcharacter if found, or a number larger than length() if no such character. You can tell which case it is by examiningStrings.Result.separator.
-
nextToken
public static final Strings.Result nextToken(java.lang.String src, int from, char[] separators) throws IllegalSyntaxException
Returns the next token with unescape.- It trims whitespaces before and after the token.
- It handles both '\'' and '"'. All characters between them are considered as a token.
- If nothing found before end-of-string, null is returned
Strings.Result.separator.- Throws:
IllegalSyntaxException- if the quoted string is unclosed.
-
nextToken
public static final Strings.Result nextToken(java.lang.String src, int from, char[] separators, boolean escBackslash, boolean quotAsToken) throws IllegalSyntaxException
Returns the next token with additional options. It is the same as nextToken(src, from, separators, escBackslash, quotAsToken, false); Refer tonextToken(String, int, char[], boolean, boolean, boolean)for more information.- Throws:
IllegalSyntaxException- if the quoted string is unclosed.- See Also:
nextToken(String, int, char[], boolean, boolean, boolean)
-
nextToken
public static final Strings.Result nextToken(java.lang.String src, int from, char[] separators, boolean escBackslash, boolean quotAsToken, boolean parenthesis) throws IllegalSyntaxException
Returns the next token with additional options.- It trims whitespaces before and after the token.
- If quotAsToken is true, all characters between quotations ('\'' or '"') are considered as a token.
- If parenthesis is true, the separators and quotes inside a pair of parenthesis won't be treated specially. It is useful if EL expressions might be contained.
- Consider '\\' as the escape char if escBackslash is true.
- If nothing found before end-of-string, null is returned
Strings.Result.separator.- Parameters:
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 ().- Throws:
IllegalSyntaxException- if the quoted string is unclosed.- Since:
- 3.0.6
-
nextSeparator
public static int nextSeparator(java.lang.String src, int from, char[] separators, boolean escBackslash, boolean escQuot, boolean quotAsSeparator)Returns the next separator index in the src string.- Parameters:
escQuot- whether to escape characters inside quotations ('\'' or '"'). In other words, ignore separators inside quotationsquotAsSeparator- whether to consider quotations as one of the separators- Since:
- 2.4.0
-
nextSeparator
public static int nextSeparator(java.lang.String src, int from, char[] separators, boolean escBackslash, boolean escQuot, boolean quotAsSeparator, boolean parenthesis)Returns the next separator index in the src string.- Parameters:
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 ().- Since:
- 3.0.6
-
join
public static final <T> java.lang.String join(T[] array)
Joins the elements of the array into a single String separated by a comma.- Parameters:
array-- Since:
- 8.0.0
-
join
public static final <T> java.lang.String join(T[] array, char separator)Joins the elements of the array into a single String separated by the given separator.- Parameters:
array-separator-- Since:
- 8.0.0
-
join
public static final <T> java.lang.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.- Parameters:
array-separator-startIndex-endIndex-- Since:
- 8.0.0
-
toString
public static final java.lang.String toString(byte[] data)
Converts the given byte[] to UTF-8 string, if possible. Or return with the default charset.This is a method helper for JDK 5
- Since:
- 8.0.2
-
defaultIfEmpty
public static final java.lang.String defaultIfEmpty(java.lang.String value, java.lang.String defaultValue)Returns the passed value, or the default value if it is empty.- Parameters:
value- the valuedefaultValue- the default value- Returns:
valueif not empty.defaultValueif empty.- Since:
- 9.5.0
-
-