public class S extends Object
A utility class providing String manipulation methods. Commonly used in template engine process.
Note all methods defined in this class is null safe. if any input is null the return value is ""
An instance of this utility is exposed to any Rythm template via TemplateBase.s() method and can be used in template source code freely. E.g.
@if(s().empty(name)) { user name is empty! } | Modifier and Type | Field and Description |
|---|---|
static String |
EMPTY_STR |
static int |
IGNORECASE
The modifier used to indicate the comparison should ignore case
|
static int |
IGNORESPACE
The modifier used to indicate the comparison should ignore space
|
static S |
INSTANCE |
| Constructor and Description |
|---|
S() |
| Modifier and Type | Method and Description |
|---|---|
static String |
camelCase(Object obj)
Turn an object’s String representation into Camel Case
|
static String |
capFirst(Object o)
Make the first character be uppercase of the given object’s string representation
|
static String |
capitalizeWords(Object o)
Capitalize the first character of every word of the specified object’s string representation.
|
static String[] |
divide(Object o,
String sep)
Bridge String.split() method to Object
|
static boolean |
empty(Object o)
Alias of
isEmpty(Object) |
static boolean |
empty(String s)
Alias of
isEmpty(String) |
static boolean |
endsWith(String string,
char suffix) |
static boolean |
eq(Object o1,
Object o2)
Alias of
isEqual(Object, Object) |
static boolean |
eq(String s1,
String s2)
Alias of
isEqual(String, String) |
static boolean |
eq(String s1,
String s2,
int modifier)
Alias of
isEqual(String, String, int) |
static RawData |
escape(ITemplate template,
Object o)
The template implicit argument version of
escape(Object) |
static RawData |
escape(Object o)
Return a
RawData type wrapper of an object without escaping or if the current template exists return the escape specified by the current escape scheme of the current render template |
static RawData |
escape(Object o,
Object escape)
Return a
RawData type wrapper of an object with specified escaping scheme. |
static RawData |
escapeCsv(Object o)
Alias of
escapeCSV(Object) |
static RawData |
escapeCSV(Object o)
Return a
RawData type wrapper of an object with CSV escaping |
static RawData |
escapeHtml(Object o)
Alias of
escapeHTML(Object) |
static RawData |
escapeHTML(Object o)
Return a
RawData type wrapper of an object with HTML escaping |
static RawData |
escapeJava(Object o)
Return a
RawData type wrapper of an object with Java escaping |
static RawData |
escapeJavascript(Object o)
Alias of
escapeJavaScript(Object) |
static RawData |
escapeJavaScript(Object o)
Return a
RawData type wrapper of an object with JavaScript escaping |
static RawData |
escapeJS(Object o)
Alias of
escapeJavaScript(Object) |
static RawData |
escapeJson(Object o)
Alias of
escapeCSV(Object) |
static RawData |
escapeJSON(Object o)
Return a
RawData type wrapper of an object with JSON escaping |
static RawData |
escapeRegex(Object o)
Escape for regular expression
|
static RawData |
escapeXml(Object o)
Alias of
escapeXML(Object) |
static RawData |
escapeXML(Object o)
Return a
RawData type wrapper of an object with XML escaping |
static String |
format(Date date)
Format a date with engine’s default format corresponding to the engine’s locale configured
|
static String |
format(Date date,
String pattern)
Format a date with specified pattern
|
static String |
format(Date date,
String pattern,
Locale locale)
Transformer.
|
static String |
format(Date date,
String pattern,
Locale locale,
String timezone)
Transformer.
|
static String |
format(ITemplate template,
Date date)
Format a date with specified engine’s default format corresponding to the engine’s locale configured
|
static String |
format(ITemplate template,
Date date,
String pattern)
Format a date with specified pattern
|
static String |
format(ITemplate template,
Date date,
String pattern,
Locale locale)
|
static String |
format(ITemplate template,
Date date,
String pattern,
Locale locale,
String timezone)
Format a date with specified pattern, lang, locale and timezone.
|
static String |
format(ITemplate template,
Number number)
Format number with specified template
|
static String |
format(ITemplate template,
Number number,
String pattern)
Format a number with specified engine, pattern
|
static String |
format(ITemplate template,
Number number,
String pattern,
Locale locale)
Format the number with specified template, pattern, language and locale
|
static String |
format(ITemplate template,
Object o)
Generalize format parameter for the sake of dynamic expr evaluation
|
static String |
format(ITemplate template,
Object o,
String pattern)
Generalize format parameter for the sake of dynamic evaluation
|
static String |
format(ITemplate template,
Object o,
String pattern,
Locale locale)
Generalize format parameter for the sake of dynamic evaluation
|
static String |
format(ITemplate template,
Object o,
String pattern,
Locale locale,
String timezone)
Generalize format parameter for the sake of dynamic evaluation
|
static String |
format(Number number)
Format a number using default pattern, language and locale
|
static String |
format(Number number,
String pattern)
Format a number with specified pattern
|
static String |
format(Number number,
String pattern,
Locale locale)
Format the number with specified pattern, language and locale
|
static String |
format(Object o)
Format general object: for the sake of dynamic expr evaluation
|
static String |
format(Object o,
String pattern)
Generalize format parameter for the sake of dynamic evaluation
|
static String |
format(Object o,
String pattern,
Locale locale)
Generalize format parameter for the sake of dynamic evaluation
|
static String |
format(Object o,
String pattern,
Locale locale,
String timezone)
Generalize format parameter for the sake of dynamic evaluation
|
static String |
formatCurrency(ITemplate template,
Object data)
|
static String |
formatCurrency(ITemplate template,
Object data,
String currencyCode)
Transformer method.
|
static String |
formatCurrency(ITemplate template,
Object data,
String currencyCode,
Locale locale)
Format give data into currency using locale info from the engine specified
|
static String |
formatCurrency(Object data)
Transformer method.
|
static String |
formatCurrency(Object data,
String currencyCode)
Transformer method.
|
static String |
formatCurrency(Object data,
String currencyCode,
Locale locale)
|
static String |
formatSize(Object data)
Format size (e.g.
|
static String |
i18n(ITemplate template,
Object key,
Object... args)
Return i18n message of a given key and args, use the locale info from the template specified.
|
static String |
i18n(Object key) |
static String |
i18n(Object key,
Object... args)
Transformer method.
|
static boolean |
isDigitsOrAlphabetic(char c)
check the given char to be a digit or alphabetic
|
static boolean |
isEmpty(Object o)
Determine if a given Object instance is null or empty after it converted to a String.
|
static boolean |
isEmpty(String s)
Determine if a given String is null or empty.
|
static boolean |
isEqual(Object o1,
Object o2)
Check if two Object is equal after converted into String
|
static boolean |
isEqual(String s1,
String s2)
Check if two String is equal.
|
static boolean |
isEqual(String s1,
String s2,
int modifier)
Determine whether two string instance is equal based on the modifier passed in.
|
static boolean |
isNotEmpty(Object o)
Determine if a given Object instance is NOT null or empty.
|
static boolean |
isNotEmpty(String s)
Determine if a given String is NOT null or empty.
|
static boolean |
isNotEqual(Object o1,
Object o2)
Check if two Object is not equal after converted into String
|
static boolean |
isNotEqual(String s1,
String s2)
Check if two String is not equal.
|
static boolean |
isNotEqual(String s1,
String s2,
int modifier)
The counter function of
isEqual(String, String, int) |
static String |
join(Character[] a) |
static String |
join(char sep,
Character[] a) |
static String |
join(char sep,
Double[] a) |
static String |
join(char sep,
Float[] a) |
static String |
join(char sep,
Integer[] a) |
static String |
join(char sep,
Iterable itr)
Join an
iterable with a char separator |
static String |
join(char sep,
Long[] a) |
static String |
join(Double[] a) |
static String |
join(Float[] a) |
static String |
join(Integer[] a) |
static String |
join(Iterable itr)
Join items in an
iterable with “,” |
static String |
join(Long[] a) |
static String |
join(String sep,
Character[] a) |
static String |
join(String sep,
Double[] a) |
static String |
join(String sep,
Float[] a) |
static String |
join(String sep,
Integer[] a) |
static String |
join(String sep,
Iterable itr)
Join an
iterable with separator |
static String |
join(String sep,
Iterator i) |
static String |
join(String sep,
Long[] a) |
static String |
join(String sep,
Object obj) |
static int |
len(Object o)
get length of specified object
|
static String |
longDate(Date date) |
static String |
longDate(ITemplate template,
Date date) |
static String |
longDateTime(Date date) |
static String |
longDateTime(ITemplate template,
Date date) |
static String |
longTime(Date date) |
static String |
longTime(ITemplate template,
Date date) |
static String |
lowerCase(Object o)
Bridge String.toUpperCase() method to Object
|
static String |
lowerFirst(Object o)
Make the first character be lowercase of the given object’s string representation
|
static String |
mediumDate(Date date) |
static String |
mediumDate(ITemplate template,
Date date) |
static String |
mediumDateTime(Date date) |
static String |
mediumDateTime(ITemplate template,
Date date) |
static String |
mediumTime(Date date) |
static String |
mediumTime(ITemplate template,
Date date) |
static boolean |
ne(Object o1,
Object o2)
Alias of
isNotEqual(Object, Object) |
static boolean |
ne(String s1,
String s2)
Alias of
isNotEqual(String, String) |
static boolean |
ne(String s1,
String s2,
int modifier)
Alias of
isNotEqual(String, String, int) |
static RawData |
nl2br(Object data)
Change line break in the data string into
|
static RawData |
nl2br(RawData data)
Change line break in the data string into
|
static String |
noAccents(Object o)
Replace accent character (usually found in European languages) of the String representation of a give object to non-accent char.
|
static boolean |
notEmpty(Object o)
Alias of
isNotEmpty(Object) |
static boolean |
notEmpty(String s)
Alias of
isNotEmpty(String) |
static String |
pathConcat(String prefix,
char sep,
String suffix) |
static String |
random()
Return a random string with 8 chars
|
static String |
random(int len)
Generate random string.
|
static RawData |
raw(Object o)
Return a
RawData type wrapper of an object without any escaping. |
static String |
removeAllLineBreaks(Object o)
Remove all line breaks from string representation of specified object O
|
static String |
shortDate(Date date) |
static String |
shortDate(ITemplate template,
Date date) |
static String |
shortDateTime(Date date) |
static String |
shortDateTime(ITemplate template,
Date date) |
static String |
shortTime(Date date) |
static String |
shortTime(ITemplate template,
Date date) |
static String |
shrinkSpace(Object o)
Shrink spaces in an object’s string representation by merge multiple spaces, tabs into one space, and multiple line breaks into one line break
|
static RawData |
sp2nbsp(Object data)
Change space in the data string into  
|
static RawData |
sp2nbsp(RawData data)
Change space in the data string into
|
static boolean |
startsWith(String string,
char prefix) |
static String |
str(Object o)
Alias of
toString(Object) |
static String |
string(Object o)
Alias of
toString(Object) |
static String |
strip(Object o,
String prefix,
String suffix)
Strip the prefix and suffix from an object’s String representation and return the result
|
static String |
stripBrace(Object o)
Strip the brace from an object’s string representation and return the result
|
static String |
stripBraceAndQuotation(Object o)
Strip off both brace and quotation
|
static String |
stripQuotation(Object o)
Strip the quotation mark from an object’s string representation and return the result
|
static String |
toJSON(Object data) |
static String |
toString(Object o)
Safe convert an Object to String.
|
static String |
upperCase(Object o)
Bridge String.toUpperCase() method to Object
|
static String |
urlEncode(Object data)
encode using utf-8
|
public static final S INSTANCE
public static final String EMPTY_STR
public static final int IGNORECASE
The modifier used to indicate the comparison should ignore case
public static final int IGNORESPACE
The modifier used to indicate the comparison should ignore space
public static boolean isEmpty(String s)
Determine if a given String is null or empty. By empty it means equals to "" after do a String.trim() operation on it
s - public static boolean empty(String s)
Alias of isEmpty(String)
s - public static boolean isNotEmpty(String s)
Determine if a given String is NOT null or empty.
s - isEmpty(String)public static boolean notEmpty(String s)
Alias of isNotEmpty(String)
s - public static boolean isEmpty(Object o)
Determine if a given Object instance is null or empty after it converted to a String.
o - isEmpty(String)public static boolean empty(Object o)
Alias of isEmpty(Object)
o - public static boolean isNotEmpty(Object o)
Determine if a given Object instance is NOT null or empty.
o - isEmpty(Object)public static boolean notEmpty(Object o)
Alias of isNotEmpty(Object)
o - public static boolean isEqual(String s1, String s2)
Check if two String is equal. This comparison is case sensitive and space sensitive
s1 - s2 - public static boolean isNotEqual(String s1, String s2)
Check if two String is not equal. This comparison is case sensitive and space sensitive
s1 - s2 - public static boolean eq(String s1, String s2)
Alias of isEqual(String, String)
s1 - s2 - public static boolean ne(String s1, String s2)
Alias of isNotEqual(String, String)
s1 - s2 - public static boolean isEqual(Object o1, Object o2)
Check if two Object is equal after converted into String
o1 - o2 - public static boolean isNotEqual(Object o1, Object o2)
Check if two Object is not equal after converted into String
o1 - o2 - public static boolean eq(Object o1, Object o2)
Alias of isEqual(Object, Object)
o1 - o2 - public static boolean ne(Object o1, Object o2)
Alias of isNotEqual(Object, Object)
o1 - o2 - public static boolean eq(String s1, String s2, int modifier)
Alias of isEqual(String, String, int)
s1 - s2 - modifier - public static boolean ne(String s1, String s2, int modifier)
Alias of isNotEqual(String, String, int)
s1 - s2 - modifier - public static boolean isEqual(String s1, String s2, int modifier)
Determine whether two string instance is equal based on the modifier passed in.
is 2 strings equal case insensitive? S.isEqual(s1, s2, S.IGNORECASE)
is 2 strings equals case and space insensitive? S.isEqual(s1, s2, S.IGNORECASE & S.IGNORESPACE)
s1 - s2 - modifier - public static boolean isNotEqual(String s1, String s2, int modifier)
The counter function of isEqual(String, String, int)
s1 - s2 - modifier - public static String str(Object o)
Alias of toString(Object)
o - public static String string(Object o)
Alias of toString(Object)
o - public static String toString(Object o)
Safe convert an Object to String. if the Object is null than "" is returned
o - public static String removeAllLineBreaks(Object o)
Remove all line breaks from string representation of specified object O
o - public static RawData raw(Object o)
Return a RawData type wrapper of an object without any escaping.
o - public static RawData escape(Object o)
Return a RawData type wrapper of an object without escaping or if the current template exists return the escape specified by the current escape scheme of the current render template
Object is converted to String before escaping
o - public static RawData escape(ITemplate template, Object o)
The template implicit argument version of escape(Object)
template - o - public static RawData escape(Object o, Object escape)
Return a RawData type wrapper of an object with specified escaping scheme.
You can pass any type of object to specify the escaping scheme. However they will in the end converted to converted to String and then determine which escaping to use:
escapeJSON(Object) escapeXML(Object) escapeJavaScript(Object) escapeCSV(Object) escapeCSV(Object) Note Object instance is converted to String before escaping
o - escape - public static RawData escapeHTML(Object o)
Return a RawData type wrapper of an object with HTML escaping
Object is converted to String before escaping
o - public static RawData escapeHtml(Object o)
Alias of escapeHTML(Object)
o - public static RawData escapeCSV(Object o)
Return a RawData type wrapper of an object with CSV escaping
Object is converted to String before escaping
o - public static RawData escapeCsv(Object o)
Alias of escapeCSV(Object)
o - public static RawData escapeJSON(Object o)
Return a RawData type wrapper of an object with JSON escaping
Object is converted to String before escaping
After the object get escaped, the output string is safe to put into a JSON block
o - public static RawData escapeJson(Object o)
Alias of escapeCSV(Object)
o - public static RawData escapeJavaScript(Object o)
Return a RawData type wrapper of an object with JavaScript escaping
Object is converted to String before escaping
After the object get escaped, the output string is safe to put inside a pair of JavaScript quotation marks
o - public static RawData escapeJavascript(Object o)
Alias of escapeJavaScript(Object)
o - public static RawData escapeJS(Object o)
Alias of escapeJavaScript(Object)
o - public static RawData escapeJava(Object o)
Return a RawData type wrapper of an object with Java escaping
Object is converted to String before escaping
After the object get escaped, the output string is safe to put inside a pair of Java quotation marks
o - public static RawData escapeXML(Object o)
Return a RawData type wrapper of an object with XML escaping
Object is converted to String before escaping
After the object get escaped, the output string is safe to put inside a XML attribute
o - public static RawData escapeXml(Object o)
Alias of escapeXML(Object)
o - public static RawData escapeRegex(Object o)
Escape for regular expression
o - public static String strip(Object o, String prefix, String suffix)
Strip the prefix and suffix from an object’s String representation and return the result
For example:
Object o = “xxBByy”; String s = S.strip(o, “xx”, “yy”) At the end above code, s should be “BB”
o - prefix - suffix - public static String stripBrace(Object o)
Strip the brace from an object’s string representation and return the result
o - public static String stripQuotation(Object o)
Strip the quotation mark from an object’s string representation and return the result
o - public static String stripBraceAndQuotation(Object o)
Strip off both brace and quotation
o - public static String shrinkSpace(Object o)
Shrink spaces in an object’s string representation by merge multiple spaces, tabs into one space, and multiple line breaks into one line break
o - public static boolean isDigitsOrAlphabetic(char c)
check the given char to be a digit or alphabetic
c - public static String capitalizeWords(Object o)
Capitalize the first character of every word of the specified object’s string representation. Words are separated by space
o - public static String noAccents(Object o)
Replace accent character (usually found in European languages) of the String representation of a give object to non-accent char.
o - public static String lowerFirst(Object o)
Make the first character be lowercase of the given object’s string representation
o - public static String capFirst(Object o)
Make the first character be uppercase of the given object’s string representation
o - public static String camelCase(Object obj)
Turn an object’s String representation into Camel Case
obj - public static String[] divide(Object o, String sep)
Bridge String.split() method to Object
o - sep - public static String lowerCase(Object o)
Bridge String.toUpperCase() method to Object
o - public static String upperCase(Object o)
Bridge String.toUpperCase() method to Object
o - public static int len(Object o)
get length of specified object
o - public static RawData nl2br(RawData data)
Change line break in the data string into
data - public static RawData nl2br(Object data)
Change line break in the data string into
data - public static RawData sp2nbsp(RawData data)
Change space in the data string into
data - public static RawData sp2nbsp(Object data)
Change space in the data string into  
data - public static String urlEncode(Object data)
encode using utf-8
data - public static String format(Number number)
Format a number using default pattern, language and locale
number - public static String format(ITemplate template, Number number)
Format number with specified template
template - number - public static String format(Number number, String pattern, Locale locale)
Format the number with specified pattern, language and locale
number - pattern - locale - DecimalFormatSymbolspublic static String format(ITemplate template, Number number, String pattern, Locale locale)
Format the number with specified template, pattern, language and locale
number - pattern - locale - DecimalFormatSymbolspublic static String format(Number number, String pattern)
Format a number with specified pattern
number - pattern - public static String format(ITemplate template, Number number, String pattern)
Format a number with specified engine, pattern
number - pattern - public static String format(Object o)
Format general object: for the sake of dynamic expr evaluation
o - public static String format(Date date)
Format a date with engine’s default format corresponding to the engine’s locale configured
date - public static String format(ITemplate template, Object o)
Generalize format parameter for the sake of dynamic expr evaluation
template - o - public static String format(ITemplate template, Date date)
Format a date with specified engine’s default format corresponding to the engine’s locale configured
date - public static String format(Date date, String pattern)
Format a date with specified pattern
date - pattern - public static String format(Object o, String pattern)
Generalize format parameter for the sake of dynamic evaluation
o - pattern - public static String format(ITemplate template, Date date, String pattern)
Format a date with specified pattern
template - date - pattern - public static String format(ITemplate template, Object o, String pattern)
Generalize format parameter for the sake of dynamic evaluation
o - pattern - public static String format(Date date, String pattern, Locale locale)
Transformer. Format a date with specified pattern, language and locale
date - pattern - locale - public static String format(Object o, String pattern, Locale locale)
Generalize format parameter for the sake of dynamic evaluation
o - pattern - public static String format(ITemplate template, Date date, String pattern, Locale locale)
template - date - pattern - locale - public static String format(ITemplate template, Object o, String pattern, Locale locale)
Generalize format parameter for the sake of dynamic evaluation
o - pattern - public static String format(Date date, String pattern, Locale locale, String timezone)
Transformer. Format a date with specified pattern, lang, locale and timezone.
date - pattern - locale - timezone - SimpleDateFormatpublic static String format(Object o, String pattern, Locale locale, String timezone)
Generalize format parameter for the sake of dynamic evaluation
o - pattern - public static String format(ITemplate template, Date date, String pattern, Locale locale, String timezone)
Format a date with specified pattern, lang, locale and timezone. The locale comes from the engine instance specified
template - date - pattern - locale - timezone - public static String format(ITemplate template, Object o, String pattern, Locale locale, String timezone)
Generalize format parameter for the sake of dynamic evaluation
o - pattern - public static String formatSize(Object data)
Format size (e.g. disk space in bytes) into human readable style
1024L, return size + B1024L ^ 2, return size/1024L + KB1024L ^ 3, return size/1048576L + MB1024L ^ 4, return size/1073741824L + GBThe method accept any data type. When null is found then
NullPointerException will be thrown out; if an Number
is passed in, it will be type cast to Long; otherwise
a Long.valueOf(data.toString()) is used to find out
the number
data - public static String formatCurrency(Object data)
Transformer method. Format given data into currency
data - formatCurrency(org.rythmengine.template.ITemplate,Object,String,java.util.Locale)public static String formatCurrency(ITemplate template, Object data)
template - data - public static String formatCurrency(Object data, String currencyCode)
Transformer method. Format currency using specified currency code
data - currencyCode - formatCurrency(org.rythmengine.template.ITemplate, Object, String, java.util.Locale)public static String formatCurrency(ITemplate template, Object data, String currencyCode)
Transformer method. Format currency using specified parameters
template - data - currencyCode - formatCurrency(org.rythmengine.template.ITemplate, Object, String, java.util.Locale)public static String formatCurrency(Object data, String currencyCode, Locale locale)
data - currencyCode - locale - public static String formatCurrency(ITemplate template, Object data, String currencyCode, Locale locale)
Format give data into currency using locale info from the engine specified
The method accept any data type. When null is found then
NullPointerException will be thrown out; if an Number
is passed in, it will be type cast to Number; otherwise
a Double.valueOf(data.toString()) is used to find out
the number
template - data - currencyCode - locale - public static String i18n(ITemplate template, Object key, Object... args)
Return i18n message of a given key and args, use the locale info from the template specified.
if null template instance passed in then it will try to guess from the current engine via
RythmEngine.get()
template - key - args - the format arguments. If the first argument is of type Locale then it will be used to specify the locale of the processing, and the rest elements are used as format argumentspublic static String i18n(Object key, Object... args)
Transformer method. Return i18n message of a given key and args.
key - args - public static String random(int len)
Generate random string.
The generated string is safe to be used as filename
len - public static String random()
Return a random string with 8 chars
public static String join(Iterable itr)
Join items in an iterable with “,”
itr - public static String join(String sep, Iterable itr)
Join an iterable with separator
itr - sep - public static String join(char sep, Iterable itr)
Join an iterable with a char separator
itr - sep - public static boolean endsWith(String string, char suffix)
public static boolean startsWith(String string, char prefix)
Copyright © 2017–2021 OSGL (Open Source General Library). All rights reserved.