Package fmpp.util
Class StringUtil
- java.lang.Object
-
- fmpp.util.StringUtil
-
public class StringUtil extends java.lang.Object
Collection of string manipulation functions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StringUtil.ParseException
Formal (syntactical) problem with the text.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
LINE_BREAK
The default line-break string used by the methods in this class.
-
Constructor Summary
Constructors Constructor Description StringUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
capitalizeFirst(java.lang.String s)
static java.lang.String
chomp(java.lang.String s)
Removes the line-break from the end of the string.static void
chomp(java.lang.StringBuffer sb)
Removes the line-break from the end of theStringBuffer
.static java.lang.String
createSourceCodeErrorMessage(java.lang.String message, java.lang.String srcCode, int position, java.lang.String fileName, int maxQuotLength)
static java.lang.String
expandTabs(java.lang.String text, int tabWidth)
Same asexpandTabs(text, tabWidth, 0)
.static java.lang.String
expandTabs(java.lang.String text, int tabWidth, int startCol)
Replaces all occurances of character tab with spaces.static java.lang.String
ftlStringLiteralDec(java.lang.String s)
FTL string literal decoding.static java.lang.String
htmlEnc(java.lang.String s)
HTML encoding (does not convert line breaks).static java.lang.String
jQuote(char c)
Quotes string as Java language character.static java.lang.String
jQuote(java.lang.String s)
Quotes string as Java language string literal.static java.lang.String
jQuoteOrName(char c)
Quotes character as Java language character, except quote characters, which are referred with name.static java.lang.String
normalizeLinebreaks(java.lang.String s)
Converts all line-breaks to UN*X linebreaks ("\n"
).static java.lang.String
repeat(java.lang.String s, int n)
static java.lang.String
replace(java.lang.String text, java.lang.String oldsub, java.lang.String newsub)
Replaces all occurances of a sub-string in a string.static java.lang.String
rtfEnc(java.lang.String s)
Rich Text Format encoding (does not replace line breaks).static java.lang.String[]
split(java.lang.String s, char c)
Same assplit(s, c, false)
.static java.lang.String[]
split(java.lang.String s, char c, boolean trim)
Splits a string at the specified character, and optionally trims the items.static java.lang.String[]
split(java.lang.String s, java.lang.String sep)
Splits a string at the specified string.static java.lang.String[]
splitAtWS(java.lang.String text)
Splits a string at white-spaces.static java.math.BigDecimal
stringToBigDecimal(java.lang.String s)
Converts a string toBigDecimal
.static boolean
stringToBoolean(java.lang.String s)
static freemarker.template.TemplateDateModel
stringToDate(java.lang.String s, java.util.TimeZone tz)
Parses a date of format"yyyy-MM-dd"
or"yyyy-MM-dd z"
and returns it asTemplateDateModel
.static freemarker.template.TemplateDateModel
stringToDateTime(java.lang.String s, java.util.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 asTemplateDateModel
.static java.lang.String
stringToPerl5Regex(java.lang.String text)
Convers string to Perl 5 regular expression.static freemarker.template.TemplateDateModel
stringToTime(java.lang.String s, java.util.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 asTemplateDateModel
.static java.lang.String
urlEnc(java.lang.String s, java.lang.String enc)
URL encoding (like%20this).static java.lang.String
urlPathEnc(java.lang.String s, java.lang.String enc)
URL encoding without escaping slashes.static java.lang.StringBuffer
wrap(java.lang.StringBuffer text, int screenWidth, int firstIndent, int indent)
Hard-wraps flow-text.static java.lang.StringBuffer
wrap(java.lang.StringBuffer text, int screenWidth, int firstIndent, int indent, java.lang.String lineBreak, boolean traceMode)
Hard-wraps flow-text.static java.lang.String
wrap(java.lang.String text, int screenWidth)
Hard-wraps flow-text.static java.lang.String
wrap(java.lang.String text, int screenWidth, int indent)
Hard-wraps flow-text.static java.lang.String
wrap(java.lang.String text, int screenWidth, int firstIndent, int indent)
Hard-wraps flow-text.static java.lang.String
wrap(java.lang.String text, int screenWidth, int firstIndent, int indent, java.lang.String lineBreak)
Hard-wraps flow-text.static java.lang.String
wrap(java.lang.String text, int screenWidth, int firstIndent, int indent, java.lang.String lineBreak, boolean traceMode)
Hard-wraps flow-text.static java.lang.String
wrap(java.lang.String text, int screenWidth, int indent, java.lang.String lineBreak)
Hard-wraps flow-text.static java.lang.String
wrap(java.lang.String text, int screenWidth, java.lang.String lineBreak)
Hard-wraps flow-text.static java.lang.String
wrapTrace(java.lang.String text, int screenWidth)
Hard-wraps flow-text.static java.lang.String
xmlEnc(java.lang.String s)
XML Encoding.static java.lang.String
xmlEncNQ(java.lang.String s)
XML encoding without replacing apostrophes and quotation marks.
-
-
-
Method Detail
-
htmlEnc
public static java.lang.String htmlEnc(java.lang.String s)
HTML encoding (does not convert line breaks). Replaces all '>' '<' '&' and '"' with entity reference
-
xmlEnc
public static java.lang.String xmlEnc(java.lang.String s)
XML Encoding. Replaces all '>' '<' '&', "'" and '"' with entity reference
-
xmlEncNQ
public static java.lang.String xmlEncNQ(java.lang.String s)
XML encoding without replacing apostrophes and quotation marks.- See Also:
xmlEnc(String)
-
rtfEnc
public static java.lang.String rtfEnc(java.lang.String s)
Rich Text Format encoding (does not replace line breaks). Escapes all '\' '{' '}' and '"'
-
jQuoteOrName
public static java.lang.String jQuoteOrName(char c)
Quotes character as Java language character, except quote characters, which are referred with name.
-
jQuote
public static java.lang.String jQuote(char c)
Quotes string as Java language character.
-
jQuote
public static java.lang.String jQuote(java.lang.String s)
Quotes string as Java language string literal.
-
ftlStringLiteralDec
public static java.lang.String ftlStringLiteralDec(java.lang.String s) throws StringUtil.ParseException
FTL string literal decoding. \\, \", \', \n, \t, \r, \b and \f will be replaced according to Java rules. In additional, it knows \g, \l, \a and \{ which are replaced with <, >, & and { respectively. \x works as hexadecimal character code escape. The character codes are interpreted according to UCS basic plane (Unicode). "f\x006Fo", "f\x06Fo" and "f\x6Fo" will be "foo". "f\x006F123" will be "foo123" as the maximum number of digits is 4. All other \X (where X is any character not mentioned above or End-of-string) will cause aStringUtil.ParseException
.- Parameters:
s
- String literal without the surrounding quotation marks- Returns:
- String with all escape sequences resolved
- Throws:
StringUtil.ParseException
- if there string contains illegal escapes
-
stringToPerl5Regex
public static java.lang.String stringToPerl5Regex(java.lang.String text)
Convers string to Perl 5 regular expression. This means that regular expression metacharacters will be escaped.
-
split
public static java.lang.String[] split(java.lang.String s, char c)
Same assplit(s, c, false)
.
-
split
public static java.lang.String[] split(java.lang.String s, char c, boolean trim)
Splits a string at the specified character, and optionally trims the items.
-
split
public static java.lang.String[] split(java.lang.String s, java.lang.String sep)
Splits a string at the specified string.
-
splitAtWS
public static java.lang.String[] splitAtWS(java.lang.String text)
Splits a string at white-spaces. A continous sequence of one or more white-space is considered as a single separator. If the string is starting or ending with a separator, then that separator is silently ignored. Thus, the result array contains only trimmed non-0-length strings.
-
replace
public static java.lang.String replace(java.lang.String text, java.lang.String oldsub, java.lang.String newsub)
Replaces all occurances of a sub-string in a string.- Parameters:
text
- The string where it will replaceoldsub
withnewsub
.- Returns:
- String The string after the replacements.
-
expandTabs
public static java.lang.String expandTabs(java.lang.String text, int tabWidth)
Same asexpandTabs(text, tabWidth, 0)
.- See Also:
expandTabs(String, int, int)
-
expandTabs
public static java.lang.String expandTabs(java.lang.String text, int tabWidth, int startCol)
Replaces all occurances of character tab with spaces.- Parameters:
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.- Returns:
- String The string after the replacements.
-
chomp
public static java.lang.String chomp(java.lang.String s)
Removes the line-break from the end of the string.
-
chomp
public static void chomp(java.lang.StringBuffer sb)
Removes the line-break from the end of theStringBuffer
.
-
urlEnc
public static java.lang.String urlEnc(java.lang.String s, java.lang.String enc) throws java.io.UnsupportedEncodingException
URL encoding (like%20this).- Throws:
java.io.UnsupportedEncodingException
-
urlPathEnc
public static java.lang.String urlPathEnc(java.lang.String s, java.lang.String enc) throws java.io.UnsupportedEncodingException
URL encoding without escaping slashes.- Throws:
java.io.UnsupportedEncodingException
-
wrap
public static java.lang.String wrap(java.lang.String text, int screenWidth)
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, 0, 0, LINE_BREAK, false)
.
-
wrapTrace
public static java.lang.String wrapTrace(java.lang.String text, int screenWidth)
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, 0, 0, LINE_BREAK, true)
.
-
wrap
public static java.lang.String wrap(java.lang.String text, int screenWidth, java.lang.String lineBreak)
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, 0, 0, lineBreak, false)
.
-
wrap
public static java.lang.String wrap(java.lang.String text, int screenWidth, int indent)
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, indent, indent, LINE_BREAK, false)
.
-
wrap
public static java.lang.String wrap(java.lang.String text, int screenWidth, int firstIndent, int indent)
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, firstIndent, indent, LINE_BREAK, false)
.
-
wrap
public static java.lang.String wrap(java.lang.String text, int screenWidth, int indent, java.lang.String lineBreak)
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, indent, indent, lineBreak, false)
.
-
wrap
public static java.lang.String wrap(java.lang.String text, int screenWidth, int firstIndent, int indent, java.lang.String lineBreak)
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, firstIndent, indent, lineBreak, false)
.
-
wrap
public static java.lang.StringBuffer wrap(java.lang.StringBuffer text, int screenWidth, int firstIndent, int indent)
Hard-wraps flow-text. Uses StringBuffer-s instead of String-s. This is a convenience method that equivalent withwrap(text, screenWidth, firstIndent, indent, LINE_BREAK)
.
-
wrap
public static java.lang.String wrap(java.lang.String text, int screenWidth, int firstIndent, int indent, java.lang.String lineBreak, boolean traceMode)
Hard-wraps flow-text.- Parameters:
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 thescreenWidth
-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.- Throws:
java.lang.IllegalArgumentException
- if the number of columns remaining for the text is less than 2.
-
wrap
public static java.lang.StringBuffer wrap(java.lang.StringBuffer text, int screenWidth, int firstIndent, int indent, java.lang.String lineBreak, boolean traceMode)
Hard-wraps flow-text. Uses StringBuffer-s instead of String-s. This is the method that is internally used by all otherwrap
variations, so if you are working with StringBuffers anyway, it gives better performance.
-
createSourceCodeErrorMessage
public static java.lang.String createSourceCodeErrorMessage(java.lang.String message, java.lang.String srcCode, int position, java.lang.String fileName, int maxQuotLength)
-
stringToBigDecimal
public static java.math.BigDecimal stringToBigDecimal(java.lang.String s) throws StringUtil.ParseException
Converts a string toBigDecimal
.- Throws:
StringUtil.ParseException
-
stringToBoolean
public static boolean stringToBoolean(java.lang.String s) throws StringUtil.ParseException
- Throws:
StringUtil.ParseException
-
stringToDate
public static freemarker.template.TemplateDateModel stringToDate(java.lang.String s, java.util.TimeZone tz) throws StringUtil.ParseException
Parses a date of format"yyyy-MM-dd"
or"yyyy-MM-dd z"
and returns it asTemplateDateModel
.- Throws:
StringUtil.ParseException
-
stringToTime
public static freemarker.template.TemplateDateModel stringToTime(java.lang.String s, java.util.TimeZone tz) throws StringUtil.ParseException
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 asTemplateDateModel
.- Throws:
StringUtil.ParseException
-
stringToDateTime
public static freemarker.template.TemplateDateModel stringToDateTime(java.lang.String s, java.util.TimeZone tz) throws StringUtil.ParseException
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 asTemplateDateModel
.- Throws:
StringUtil.ParseException
-
normalizeLinebreaks
public static java.lang.String normalizeLinebreaks(java.lang.String s)
Converts all line-breaks to UN*X linebreaks ("\n"
). The input text can contain UN*X, DOS (Windows) and Mac linebreaks mixed.
-
repeat
public static java.lang.String repeat(java.lang.String s, int n)
- Since:
- 0.9.15
-
capitalizeFirst
public static java.lang.String capitalizeFirst(java.lang.String s)
- Since:
- 0.9.15
-
-