public class StringUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
StringUtil.JsStringEncCompatibility
Used as the argument of
jsStringEnc(String, JsStringEncCompatibility, JsStringEncQuotation) . |
static class |
StringUtil.JsStringEncQuotation
Used as the argument of
jsStringEnc(String, JsStringEncCompatibility, JsStringEncQuotation) . |
Constructor and Description |
---|
StringUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
capitalize(java.lang.String s) |
static java.lang.String |
chomp(java.lang.String s)
Removes a line-break from the end of the string (if there's any).
|
static java.util.Locale |
deduceLocale(java.lang.String input) |
static java.lang.String |
emptyToNull(java.lang.String s)
Converts a 0-length string to null, leaves the string as is otherwise.
|
static java.lang.String |
ftlQuote(java.lang.String s)
Creates a quoted FTL string literal from a string, using escaping where necessary.
|
static java.lang.String |
FTLStringLiteralDec(java.lang.String s)
FTL string literal decoding.
|
static java.lang.String |
FTLStringLiteralEnc(java.lang.String s)
Escapes a string according the FTL string literal escaping rules; it doesn't add the quotation marks.
|
static java.lang.String |
FTLStringLiteralEnc(java.lang.String s,
char quotation)
Escapes a string according the FTL string literal escaping rules, assuming the literal is quoted with
quotation ; it doesn't add the quotation marks itself. |
static boolean |
getYesNo(java.lang.String s) |
static java.util.regex.Pattern |
globToRegularExpression(java.lang.String glob)
|
static java.util.regex.Pattern |
globToRegularExpression(java.lang.String glob,
boolean caseInsensitive)
Creates a regular expression from a glob.
|
static java.lang.String |
HTMLEnc(java.lang.String s)
Deprecated.
Use
XHTMLEnc(String) instead, because it escapes apostrophe-quote too. |
static boolean |
isBackslashEscapedFTLIdentifierCharacter(char c)
Tells if a character can occur in an FTL identifier if it's preceded with a backslash.
|
static boolean |
isFTLIdentifierPart(char c)
Tells if a character can occur in an FTL identifier expression (without escaping) as other than the first
character.
|
static boolean |
isFTLIdentifierStart(char c)
Tells if a character can occur on the beginning of an FTL identifier expression (without escaping).
|
static boolean |
isTrimmableToEmpty(char[] text)
Tells if
String.trim() will return a 0-length string for the String equivalent of the argument. |
static boolean |
isTrimmableToEmpty(char[] text,
int start)
Like
isTrimmableToEmpty(char[]) , but acts on a sub-array that starts at start (inclusive index). |
static boolean |
isTrimmableToEmpty(char[] text,
int start,
int end)
Like
isTrimmableToEmpty(char[]) , but acts on a sub-array that starts at start (inclusive index)
and ends at end (exclusive index). |
static boolean |
isXMLID(java.lang.String name)
Deprecated.
Don't use this outside FreeMarker; it's name if misleading, and it doesn't follow the XML specs.
|
static java.lang.String |
javaScriptStringEnc(java.lang.String s)
Escapes a
String to be safely insertable into a JavaScript string literal; for more see
jsStringEnc(s, JsStringEncCompatibility.JAVA_SCRIPT, null) . |
static java.lang.String |
javaStringEnc(java.lang.String s)
Escapes the
String with the escaping rules of Java language
string literals, so it's safe to insert the value into a string literal. |
static java.lang.String |
javaStringEnc(java.lang.String s,
boolean quote)
Escapes the
String with the escaping rules of Java language string literals, and then if quote is
true, it also adds quotation marks before and after it. |
static java.lang.String |
jQuote(java.lang.Object obj)
|
static java.lang.String |
jQuote(java.lang.String s)
Quotes string as Java Language string literal.
|
static java.lang.String |
jQuoteNoXSS(java.lang.Object obj)
|
static java.lang.String |
jQuoteNoXSS(java.lang.String s)
|
static java.lang.String |
jsonStringEnc(java.lang.String s)
Escapes a
String to be safely insertable into a JSON string literal; for more see
jsStringEnc(s, JsStringEncCompatibility.JSON, null) . |
static java.lang.String |
jsStringEnc(java.lang.String s,
boolean json)
Deprecated.
Use
jsStringEnc(String, JsStringEncCompatibility) instead. |
static java.lang.String |
jsStringEnc(java.lang.String s,
StringUtil.JsStringEncCompatibility compatibility)
Escapes a
String to be safely insertable into a JSON or JavaScript string literal; for more see
jsStringEnc(s, compatibility, null) . |
static java.lang.String |
jsStringEnc(java.lang.String s,
StringUtil.JsStringEncCompatibility compatibility,
StringUtil.JsStringEncQuotation quotation)
Escapes a
String to be safely insertable into a JavaScript or a JSON string literal, and if the 3rd
argument is true , also adds quotation marks around it. |
static java.lang.String |
leftPad(java.lang.String s,
int minLength)
Pads the string at the left with spaces until it reaches the desired
length.
|
static java.lang.String |
leftPad(java.lang.String s,
int minLength,
char filling)
Pads the string at the left with the specified character until it reaches
the desired length.
|
static java.lang.String |
leftPad(java.lang.String s,
int minLength,
java.lang.String filling)
Pads the string at the left with a filling pattern until it reaches the
desired length.
|
static boolean |
matchesName(java.lang.String qname,
java.lang.String nodeName,
java.lang.String nsURI,
Environment env) |
static java.util.Map |
parseNameValuePairList(java.lang.String s,
java.lang.String defaultValue)
Parses a name-value pair list, where the pairs are separated with comma,
and the name and value is separated with colon.
|
static java.lang.String |
replace(java.lang.String text,
java.lang.String oldSub,
java.lang.String newSub)
Same as
replace(String, String, String, boolean, boolean) with two false parameters. |
static java.lang.String |
replace(java.lang.String text,
java.lang.String oldsub,
java.lang.String newsub,
boolean caseInsensitive,
boolean firstOnly)
Replaces all occurrences of a sub-string in a string.
|
static java.lang.String |
rightPad(java.lang.String s,
int minLength)
Pads the string at the right with spaces until it reaches the desired
length.
|
static java.lang.String |
rightPad(java.lang.String s,
int minLength,
char filling)
Pads the string at the right with the specified character until it
reaches the desired length.
|
static java.lang.String |
rightPad(java.lang.String s,
int minLength,
java.lang.String filling)
Pads the string at the right with a filling pattern until it reaches the
desired length.
|
static java.lang.String |
RTFEnc(java.lang.String s)
Rich Text Format encoding (does not replace line breaks).
|
static void |
RTFEnc(java.lang.String s,
java.io.Writer out)
Like
RTFEnc(String) , but writes the result into a Writer . |
static java.lang.String[] |
split(java.lang.String s,
char c)
Splits a string at the specified character.
|
static java.lang.String[] |
split(java.lang.String s,
java.lang.String sep,
boolean caseInsensitive)
Splits a string at the specified string.
|
static java.lang.String |
toLowerABC(int n)
Same as
toUpperABC(int) , but produces lower case result, like "ab" . |
static java.lang.String |
toUpperABC(int n)
Converts
1 , 2 , 3 and so forth to "A" , "B" , "C" and so fort. |
static char[] |
trim(char[] cs)
Behaves exactly like
String.trim() , but works on arrays. |
static java.lang.String |
tryToString(java.lang.Object object)
Tries to run
toString() , but if that fails, returns a
"[com.example.SomeClass.toString() failed: " + e + "]" instead. |
static java.lang.String |
URLEnc(java.lang.String s,
java.lang.String charset)
URL encoding (like%20this) for query parameter values, path segments, fragments; this encodes all
characters that are reserved anywhere.
|
static java.lang.String |
URLPathEnc(java.lang.String s,
java.lang.String charset)
Like
URLEnc(String, String) but doesn't escape the slash character (/ ). |
static int |
versionStringToInt(java.lang.String version)
Converts a version number string to an integer for easy comparison.
|
static java.lang.String |
XHTMLEnc(java.lang.String s)
XHTML Encoding.
|
static void |
XHTMLEnc(java.lang.String s,
java.io.Writer out)
Like
XHTMLEnc(String) , but writes the result into a Writer . |
static java.lang.String |
XMLEnc(java.lang.String s)
XML Encoding.
|
static void |
XMLEnc(java.lang.String s,
java.io.Writer out)
Like
XMLEnc(String) , but writes the result into a Writer . |
static java.lang.String |
XMLEncNA(java.lang.String s)
XML encoding without replacing apostrophes.
|
static java.lang.String |
XMLEncNQG(java.lang.String s)
XML encoding without replacing apostrophes and quotation marks and
greater-thans (except in
]]> ). |
static java.lang.String |
XMLEncQAttr(java.lang.String s)
XML encoding for attribute values quoted with " (not with '!).
|
@Deprecated public static java.lang.String HTMLEnc(java.lang.String s)
XHTMLEnc(String)
instead, because it escapes apostrophe-quote too.public static java.lang.String XMLEnc(java.lang.String s)
public static void XMLEnc(java.lang.String s, java.io.Writer out) throws java.io.IOException
XMLEnc(String)
, but writes the result into a Writer
.java.io.IOException
public static java.lang.String XHTMLEnc(java.lang.String s)
public static void XHTMLEnc(java.lang.String s, java.io.Writer out) throws java.io.IOException
XHTMLEnc(String)
, but writes the result into a Writer
.java.io.IOException
public static java.lang.String XMLEncNA(java.lang.String s)
XMLEnc(String)
public static java.lang.String XMLEncQAttr(java.lang.String s)
XMLEnc(String)
public static java.lang.String XMLEncNQG(java.lang.String s)
]]>
).XMLEnc(String)
public static java.lang.String RTFEnc(java.lang.String s)
public static void RTFEnc(java.lang.String s, java.io.Writer out) throws java.io.IOException
RTFEnc(String)
, but writes the result into a Writer
.java.io.IOException
public static java.lang.String URLEnc(java.lang.String s, java.lang.String charset) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public static java.lang.String URLPathEnc(java.lang.String s, java.lang.String charset) throws java.io.UnsupportedEncodingException
URLEnc(String, String)
but doesn't escape the slash character (/
).
This can be used to encode a path only if you know that no folder or file name will contain /
character (not in the path, but in the name itself), which usually stands, as the commonly used OS-es don't
allow that.java.io.UnsupportedEncodingException
public static java.lang.String FTLStringLiteralEnc(java.lang.String s, char quotation)
quotation
; it doesn't add the quotation marks itself.quotation
- Either '"'
or '\''
. It's assumed that the string literal whose part we calculate is
enclosed within this kind of quotation mark. Thus, the other kind of quotation character will not be
escaped in the result.public static java.lang.String FTLStringLiteralEnc(java.lang.String s)
FTLStringLiteralEnc(String, char)
public static java.lang.String FTLStringLiteralDec(java.lang.String s) throws ParseException
s
- String literal without the surrounding quotation marksParseException
- if there string contains illegal escapespublic static java.util.Locale deduceLocale(java.lang.String input)
public static java.lang.String capitalize(java.lang.String s)
public static boolean getYesNo(java.lang.String s)
public static java.lang.String[] split(java.lang.String s, char c)
public static java.lang.String[] split(java.lang.String s, java.lang.String sep, boolean caseInsensitive)
sep
- The string that separates the items of the resulting array. Since 2.3.28, if this is 0 length, then
each character will be a separate item in the array.public static java.lang.String replace(java.lang.String text, java.lang.String oldSub, java.lang.String newSub)
replace(String, String, String, boolean, boolean)
with two false
parameters.public static java.lang.String replace(java.lang.String text, java.lang.String oldsub, java.lang.String newsub, boolean caseInsensitive, boolean firstOnly)
text
- The string where it will replace oldsub
with
newsub
.public static java.lang.String chomp(java.lang.String s)
public static java.lang.String emptyToNull(java.lang.String s)
s
- maybe null
.public static java.lang.String jQuote(java.lang.Object obj)
public static java.lang.String jQuote(java.lang.String s)
"null"
if s
is null
.public static java.lang.String jQuoteNoXSS(java.lang.Object obj)
public static java.lang.String jQuoteNoXSS(java.lang.String s)
jQuoteNoXSS(String)
but also escapes '<'
as \
u003C
. This is used for log messages to prevent XSS
on poorly written Web-based log viewers.public static java.lang.String ftlQuote(java.lang.String s)
s
- The value that should be converted to an FTL string literal whose evaluated value equals to s
public static boolean isFTLIdentifierStart(char c)
public static boolean isFTLIdentifierPart(char c)
public static boolean isBackslashEscapedFTLIdentifierCharacter(char c)
"-"
is a such character (as you can have an identifier like foo\-bar
in FTL), but "f"
is not, as
it needn't be, and can't be escaped.public static java.lang.String javaStringEnc(java.lang.String s)
String
with the escaping rules of Java language
string literals, so it's safe to insert the value into a string literal.
The resulting string will not be quoted.
See more details at javaStringEnc(String, boolean)
, as this just calls that with false
as the
2nd argument.public static java.lang.String javaStringEnc(java.lang.String s, boolean quote)
String
with the escaping rules of Java language string literals, and then if quote
is
true, it also adds quotation marks before and after it.
All characters under UCS code point 0x20 will be escaped. Where they have no dedicated escape sequence in Java, they will be replaced with hexadecimal escape (\uXXXX).
jQuote(String)
public static java.lang.String javaScriptStringEnc(java.lang.String s)
String
to be safely insertable into a JavaScript string literal; for more see
jsStringEnc(s, JsStringEncCompatibility.JAVA_SCRIPT, null)
.public static java.lang.String jsonStringEnc(java.lang.String s)
String
to be safely insertable into a JSON string literal; for more see
jsStringEnc(s, JsStringEncCompatibility.JSON, null)
.@Deprecated public static java.lang.String jsStringEnc(java.lang.String s, boolean json)
jsStringEnc(String, JsStringEncCompatibility)
instead.String
to be safely insertable into a JSON or JavaScript string literal; for more see
jsStringEnc(s, json ? JsStringEncCompatibility.JSON : JsStringEncCompatibility.JAVA_SCRIPT, null)
.public static java.lang.String jsStringEnc(java.lang.String s, StringUtil.JsStringEncCompatibility compatibility)
String
to be safely insertable into a JSON or JavaScript string literal; for more see
jsStringEnc(s, compatibility, null)
.public static java.lang.String jsStringEnc(java.lang.String s, StringUtil.JsStringEncCompatibility compatibility, StringUtil.JsStringEncQuotation quotation)
String
to be safely insertable into a JavaScript or a JSON string literal, and if the 3rd
argument is true
, also adds quotation marks around it.
If instead the caller ensures that the quotation marks are there, then in JSON mode (2nd argument), the quotation
marks must be "
, not '
, because for JSON we won't escape '
.
The escaping rules guarantee that if the inside of the JavaScript/JSON string literal is from one or more
touching pieces that were escaped with this, no character sequence can occur that closes the
JavaScript/JSON string literal, or has a meaning in HTML/XML that causes the HTML script section to be closed.
(If, however, the escaped section is preceded by or followed by strings from other sources, this can't be
guaranteed in some rare cases. Like x = "</${a?js_string}" might closes the "script"
element if a
is "script>"
.)
The escaped characters are:
Input | Output |
---|---|
" | \" |
' if not in JSON-mode, nor is the quited argument true
| \' |
\ | \\ |
/ if the method can't know that it won't be directly after < | \/ |
> if the method can't know that it won't be directly after ]] or -- | JavaScript: \>; JSON: \u003E |
< if the method can't know that it won't be directly followed by ! or ? | \u003C |
u0000-u001f (UNICODE control characters - disallowed by JSON) u007f-u009f (UNICODE control characters - disallowed by JSON) | \n, \r and such, or if there's no such dedicated escape: JavaScript: \xXX, JSON: \uXXXX |
u2028 (Line separator - source code line-break in ECMAScript) u2029 (Paragraph separator - source code line-break in ECMAScript) | \uXXXX |
s
- The string to escapecompatibility
- If escaping should restrict itself to rules that are valid in JSON, in JavaScript, or in both.quotation
- In not null
, quotation marks of this type are added around the value.public static java.util.Map parseNameValuePairList(java.lang.String s, java.lang.String defaultValue) throws java.text.ParseException
defaultValue
is not null. When a
value is omitted, then the colon after the key must be omitted as well.
The same key can't be used for multiple times.s
- the string to parse.
For example: "strong:100, soft:900"
.defaultValue
- the value used when the value is omitted in a
key-value pair.java.text.ParseException
- if the string is not a valid name-value
pair list.@Deprecated public static boolean isXMLID(java.lang.String name)
public static boolean matchesName(java.lang.String qname, java.lang.String nodeName, java.lang.String nsURI, Environment env)
public static java.lang.String leftPad(java.lang.String s, int minLength)
s
- the string that will be padded.minLength
- the length to reach.public static java.lang.String leftPad(java.lang.String s, int minLength, char filling)
s
- the string that will be padded.minLength
- the length to reach.filling
- the filling pattern.public static java.lang.String leftPad(java.lang.String s, int minLength, java.lang.String filling)
leftPad('ABC', 9, '1234')
returns "123412ABC"
.s
- the string that will be padded.minLength
- the length to reach.filling
- the filling pattern. Must be at least 1 characters long.
Can't be null
.public static java.lang.String rightPad(java.lang.String s, int minLength)
s
- the string that will be padded.minLength
- the length to reach.public static java.lang.String rightPad(java.lang.String s, int minLength, char filling)
s
- the string that will be padded.minLength
- the length to reach.filling
- the filling pattern.public static java.lang.String rightPad(java.lang.String s, int minLength, java.lang.String filling)
rightPad('ABC', 9, '1234')
returns "ABC412341"
. Note that the filling pattern is
started as if you overlay "123412341"
with the left-aligned
"ABC"
, so it starts with "4"
.s
- the string that will be padded.minLength
- the length to reach.filling
- the filling pattern. Must be at least 1 characters long.
Can't be null
.public static int versionStringToInt(java.lang.String version)
public static java.lang.String tryToString(java.lang.Object object)
toString()
, but if that fails, returns a
"[com.example.SomeClass.toString() failed: " + e + "]"
instead. Also, it returns null
for
null
parameter.public static java.lang.String toUpperABC(int n)
1
, 2
, 3
and so forth to "A"
, "B"
, "C"
and so fort. When
reaching "Z"
, it continues like "AA"
, "AB"
, etc. The lowest supported number is 1, but
there's no upper limit.java.lang.IllegalArgumentException
- If the argument is 0 or less.public static java.lang.String toLowerABC(int n)
toUpperABC(int)
, but produces lower case result, like "ab"
.public static char[] trim(char[] cs)
String.trim()
, but works on arrays. If the resulting array would have the same
content after trimming, it returns the original array instance. Otherwise it returns a new array instance (or
CollectionUtils.EMPTY_CHAR_ARRAY
).public static boolean isTrimmableToEmpty(char[] text)
String.trim()
will return a 0-length string for the String
equivalent of the argument.public static boolean isTrimmableToEmpty(char[] text, int start)
isTrimmableToEmpty(char[])
, but acts on a sub-array that starts at start
(inclusive index).public static boolean isTrimmableToEmpty(char[] text, int start, int end)
isTrimmableToEmpty(char[])
, but acts on a sub-array that starts at start
(inclusive index)
and ends at end
(exclusive index).public static java.util.regex.Pattern globToRegularExpression(java.lang.String glob)
public static java.util.regex.Pattern globToRegularExpression(java.lang.String glob, boolean caseInsensitive)
/
for as file separator, not \
(backslash), and is always case sensitive.
This glob implementation recognizes these special characters:
?
: Wildcard that matches exactly one character, other than /
*
: Wildcard that matches zero, one or multiple characters, other than /
**
: Wildcard that matches zero, one or multiple directories. For example, **
/head.ftl
matches foo/bar/head.ftl
, foo/head.ftl
and head.ftl
too. **
must be either
preceded by /
or be at the beginning of the glob. **
must be either followed by /
or be
at the end of the glob. When **
is at the end of the glob, it also matches file names, like
a/**
matches a/b/c.ftl
. If the glob only consist of a **
, it will be a match for
everything.
\
(backslash): Makes the next character non-special (a literal). For example How\?.ftl
will
match How?.ftl
, but not HowX.ftl
. Naturally, two backslashes produce one literal backslash.
[
: Reserved for future purposes; can't be used
{
: Reserved for future purposes; can't be used