public class StringUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
StringUtil.ParseException
Formal (syntactical) problem with the text.
|
Modifier and Type | Field and Description |
---|---|
static String |
LINE_BREAK
The default line-break string used by the methods in this class.
|
Constructor and Description |
---|
StringUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
capitalizeFirst(String s) |
static String |
chomp(String s)
Removes the line-break from the end of the string.
|
static void |
chomp(StringBuffer sb)
Removes the line-break from the end of the
StringBuffer . |
static String |
createSourceCodeErrorMessage(String message,
String srcCode,
int position,
String fileName,
int maxQuotLength) |
static String |
expandTabs(String text,
int tabWidth)
Same as
expandTabs(text, tabWidth, 0) . |
static String |
expandTabs(String text,
int tabWidth,
int startCol)
Replaces all occurances of character tab with spaces.
|
static String |
ftlStringLiteralDec(String s)
FTL string literal decoding.
|
static String |
htmlEnc(String s)
HTML encoding (does not convert line breaks).
|
static String |
jQuote(char c)
Quotes string as Java language character.
|
static String |
jQuote(String s)
Quotes string as Java language string literal.
|
static String |
jQuoteOrName(char c)
Quotes character as Java language character, except quote characters,
which are referred with name.
|
static String |
normalizeLinebreaks(String s)
Converts all line-breaks to UN*X linebreaks (
"\n" ). |
static String |
repeat(String s,
int n) |
static String |
replace(String text,
String oldsub,
String newsub)
Replaces all occurances of a sub-string in a string.
|
static String |
rtfEnc(String s)
Rich Text Format encoding (does not replace line breaks).
|
static String[] |
split(String s,
char c)
Same as
split(s, c, false) . |
static String[] |
split(String s,
char c,
boolean trim)
Splits a string at the specified character, and optionally trims the
items.
|
static String[] |
split(String s,
String sep)
Splits a string at the specified string.
|
static String[] |
splitAtWS(String text)
Splits a string at white-spaces.
|
static BigDecimal |
stringToBigDecimal(String s)
Converts a string to
BigDecimal . |
static boolean |
stringToBoolean(String s) |
static TemplateDateModel |
stringToDate(String s,
TimeZone tz)
|
static TemplateDateModel |
stringToDateTime(String s,
TimeZone tz)
Parses a date-time of format
"yyyy-MM-dd H:mm:ss"
or "yyyy-MM-dd h:mm:ss a" or
"yyyy-MM-dd H:mm:ss z"
or "yyyy-MM-dd h:mm:ss a z" and returns it as
TemplateDateModel . |
static String |
stringToPerl5Regex(String text)
Convers string to Perl 5 regular expression.
|
static TemplateDateModel |
stringToTime(String s,
TimeZone tz)
Parses a time of format
"H:mm:ss"
or "h:mm:ss a" or "H:mm:ss z"
or "h:mm:ss a z" and returns it as
TemplateDateModel . |
static String |
urlEnc(String s,
String enc)
URL encoding (like%20this).
|
static String |
urlPathEnc(String s,
String enc)
URL encoding without escaping slashes.
|
static StringBuffer |
wrap(StringBuffer text,
int screenWidth,
int firstIndent,
int indent)
Hard-wraps flow-text.
|
static StringBuffer |
wrap(StringBuffer text,
int screenWidth,
int firstIndent,
int indent,
String lineBreak,
boolean traceMode)
Hard-wraps flow-text.
|
static String |
wrap(String text,
int screenWidth)
Hard-wraps flow-text.
|
static String |
wrap(String text,
int screenWidth,
int indent)
Hard-wraps flow-text.
|
static String |
wrap(String text,
int screenWidth,
int firstIndent,
int indent)
Hard-wraps flow-text.
|
static String |
wrap(String text,
int screenWidth,
int firstIndent,
int indent,
String lineBreak)
Hard-wraps flow-text.
|
static String |
wrap(String text,
int screenWidth,
int firstIndent,
int indent,
String lineBreak,
boolean traceMode)
Hard-wraps flow-text.
|
static String |
wrap(String text,
int screenWidth,
int indent,
String lineBreak)
Hard-wraps flow-text.
|
static String |
wrap(String text,
int screenWidth,
String lineBreak)
Hard-wraps flow-text.
|
static String |
wrapTrace(String text,
int screenWidth)
Hard-wraps flow-text.
|
static String |
xmlEnc(String s)
XML Encoding.
|
static String |
xmlEncNQ(String s)
XML encoding without replacing apostrophes and quotation marks.
|
public static final String LINE_BREAK
public static String htmlEnc(String s)
public static String xmlEnc(String s)
public static String xmlEncNQ(String s)
xmlEnc(String)
public static String rtfEnc(String s)
public static String jQuoteOrName(char c)
public static String jQuote(char c)
public static String ftlStringLiteralDec(String s) throws StringUtil.ParseException
StringUtil.ParseException
.s
- String literal without the surrounding quotation marksStringUtil.ParseException
- if there string contains illegal escapespublic static String stringToPerl5Regex(String text)
public static String[] split(String s, char c)
split(s, c, false)
.public static String[] split(String s, char c, boolean trim)
public static String[] splitAtWS(String text)
public static String replace(String text, String oldsub, String newsub)
text
- The string where it will replace oldsub
with
newsub
.public static String expandTabs(String text, int tabWidth)
expandTabs(text, tabWidth, 0)
.expandTabs(String, int, int)
public static String expandTabs(String text, int tabWidth, int startCol)
tabWidth
- the distance of tab stops.startCol
- the index of the column in which the first character of
the string is from the left edge of the page. The index of the first
column is 0.public static void chomp(StringBuffer sb)
StringBuffer
.public static String urlEnc(String s, String enc) throws UnsupportedEncodingException
UnsupportedEncodingException
public static String urlPathEnc(String s, String enc) throws UnsupportedEncodingException
UnsupportedEncodingException
public static String wrap(String text, int screenWidth)
wrap(text, screenWidth, 0, 0, LINE_BREAK, false)
.public static String wrapTrace(String text, int screenWidth)
wrap(text, screenWidth, 0, 0, LINE_BREAK, true)
.public static String wrap(String text, int screenWidth, String lineBreak)
wrap(text, screenWidth, 0, 0, lineBreak, false)
.public static String wrap(String text, int screenWidth, int indent)
wrap(text, screenWidth, indent, indent, LINE_BREAK, false)
.public static String wrap(String text, int screenWidth, int firstIndent, int indent)
wrap(text, screenWidth, firstIndent, indent, LINE_BREAK,
false)
.public static String wrap(String text, int screenWidth, int indent, String lineBreak)
wrap(text, screenWidth, indent, indent, lineBreak, false)
.public static String wrap(String text, int screenWidth, int firstIndent, int indent, String lineBreak)
wrap(text, screenWidth, firstIndent, indent, lineBreak,
false)
.public static StringBuffer wrap(StringBuffer text, int screenWidth, int firstIndent, int indent)
wrap(text, screenWidth, firstIndent, indent, LINE_BREAK)
.public static String wrap(String text, int screenWidth, int firstIndent, int indent, String lineBreak, boolean traceMode)
text
- The flow-text to wrap. The explicit line-breaks of the
source text will be kept. All types of line-breaks (UN*X, Mac, DOS/Win)
are understood.screenWidth
- The (minimum) width of the screen. It does not
utilize the screenWidth
-th column of the screen to store
characters, except line-breaks (because some terminals/editors
do an automatic line-break when you write visible character there,
and some doesn't... so it is unpredictable if an explicit line-break
is needed or not.).firstIndent
- The indentation of the first lineindent
- The indentation of all lines but the first linelineBreak
- The String used for line-breakstraceMode
- Set this true if the input text is a Java stack
trace. In this mode, all lines starting with
optional indentation + 'at'
+ space are treated as location
lines, and will be indented and wrapped in a slightly special way.IllegalArgumentException
- if the number of columns remaining for
the text is less than 2.public static StringBuffer wrap(StringBuffer text, int screenWidth, int firstIndent, int indent, String lineBreak, boolean traceMode)
wrap
variations, so if you are working with StringBuffers anyway, it gives
better performance.public static String createSourceCodeErrorMessage(String message, String srcCode, int position, String fileName, int maxQuotLength)
public static BigDecimal stringToBigDecimal(String s) throws StringUtil.ParseException
BigDecimal
.StringUtil.ParseException
public static boolean stringToBoolean(String s) throws StringUtil.ParseException
StringUtil.ParseException
public static TemplateDateModel stringToDate(String s, TimeZone tz) throws StringUtil.ParseException
StringUtil.ParseException
public static TemplateDateModel stringToTime(String s, TimeZone tz) throws StringUtil.ParseException
"H:mm:ss"
or "h:mm:ss a"
or "H:mm:ss z"
or "h:mm:ss a z"
and returns it as
TemplateDateModel
.StringUtil.ParseException
public static TemplateDateModel stringToDateTime(String s, TimeZone tz) throws StringUtil.ParseException
"yyyy-MM-dd H:mm:ss"
or "yyyy-MM-dd h:mm:ss a"
or
"yyyy-MM-dd H:mm:ss z"
or "yyyy-MM-dd h:mm:ss a z"
and returns it as
TemplateDateModel
.StringUtil.ParseException
public static String normalizeLinebreaks(String s)
"\n"
). The
input text can contain UN*X, DOS (Windows) and Mac linebreaks mixed.