Package fmpp.util
Class StringUtil
java.lang.Object
fmpp.util.StringUtil
Collection of string manipulation functions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Formal (syntactical) problem with the text. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The default line-break string used by the methods in this class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
static String
Removes the line-break from the end of the string.static void
chomp
(StringBuffer sb) Removes the line-break from the end of theStringBuffer
.static String
createSourceCodeErrorMessage
(String message, String srcCode, int position, String fileName, int maxQuotLength) static String
expandTabs
(String text, int tabWidth) Same asexpandTabs(text, tabWidth, 0)
.static String
expandTabs
(String text, int tabWidth, int startCol) Replaces all occurances of character tab with spaces.static String
FTL string literal decoding.static String
HTML encoding (does not convert line breaks).static String
jQuote
(char c) Quotes string as Java language character.static String
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
Converts all line-breaks to UN*X linebreaks ("\n"
).static String
static String
Replaces all occurances of a sub-string in a string.static String
Rich Text Format encoding (does not replace line breaks).static String[]
Same assplit(s, c, false)
.static String[]
Splits a string at the specified character, and optionally trims the items.static String[]
Splits a string at the specified string.static String[]
Splits a string at white-spaces.static BigDecimal
Converts a string toBigDecimal
.static boolean
static freemarker.template.TemplateDateModel
stringToDate
(String s, TimeZone tz) Parses a date of format"yyyy-MM-dd"
or"yyyy-MM-dd z"
and returns it asTemplateDateModel
.static freemarker.template.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 asTemplateDateModel
.static String
stringToPerl5Regex
(String text) Convers string to Perl 5 regular expression.static freemarker.template.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 asTemplateDateModel
.static String
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
Hard-wraps flow-text.static String
Hard-wraps flow-text.static String
Hard-wraps flow-text.static String
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
Hard-wraps flow-text.static String
Hard-wraps flow-text.static String
Hard-wraps flow-text.static String
XML Encoding.static String
XML encoding without replacing apostrophes and quotation marks.
-
Field Details
-
LINE_BREAK
The default line-break string used by the methods in this class.
-
-
Constructor Details
-
StringUtil
public StringUtil()
-
-
Method Details
-
htmlEnc
HTML encoding (does not convert line breaks). Replaces all '>' '<' '&' and '"' with entity reference -
xmlEnc
XML Encoding. Replaces all '>' '<' '&', "'" and '"' with entity reference -
xmlEncNQ
XML encoding without replacing apostrophes and quotation marks.- See Also:
-
rtfEnc
Rich Text Format encoding (does not replace line breaks). Escapes all '\' '{' '}' and '"' -
jQuoteOrName
Quotes character as Java language character, except quote characters, which are referred with name. -
jQuote
Quotes string as Java language character. -
jQuote
Quotes string as Java language string literal. -
ftlStringLiteralDec
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
Convers string to Perl 5 regular expression. This means that regular expression metacharacters will be escaped. -
split
Same assplit(s, c, false)
. -
split
Splits a string at the specified character, and optionally trims the items. -
split
Splits a string at the specified string. -
splitAtWS
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
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
Same asexpandTabs(text, tabWidth, 0)
.- See Also:
-
expandTabs
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
Removes the line-break from the end of the string. -
chomp
Removes the line-break from the end of theStringBuffer
. -
urlEnc
URL encoding (like%20this).- Throws:
UnsupportedEncodingException
-
urlPathEnc
URL encoding without escaping slashes.- Throws:
UnsupportedEncodingException
-
wrap
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, 0, 0, LINE_BREAK, false)
. -
wrapTrace
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, 0, 0, LINE_BREAK, true)
. -
wrap
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, 0, 0, lineBreak, false)
. -
wrap
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, indent, indent, LINE_BREAK, false)
. -
wrap
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, firstIndent, indent, LINE_BREAK, false)
. -
wrap
Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, indent, indent, lineBreak, false)
. -
wrap
public static String wrap(String text, int screenWidth, int firstIndent, int indent, String lineBreak) Hard-wraps flow-text. This is a convenience method that equivalent withwrap(text, screenWidth, firstIndent, indent, lineBreak, false)
. -
wrap
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 String wrap(String text, int screenWidth, int firstIndent, int indent, 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:
IllegalArgumentException
- if the number of columns remaining for the text is less than 2.
-
wrap
public static StringBuffer wrap(StringBuffer text, int screenWidth, int firstIndent, int indent, 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
-
stringToBigDecimal
Converts a string toBigDecimal
.- Throws:
StringUtil.ParseException
-
stringToBoolean
- Throws:
StringUtil.ParseException
-
stringToDate
public static freemarker.template.TemplateDateModel stringToDate(String s, 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(String s, 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(String s, 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
Converts all line-breaks to UN*X linebreaks ("\n"
). The input text can contain UN*X, DOS (Windows) and Mac linebreaks mixed. -
repeat
- Since:
- 0.9.15
-
capitalizeFirst
- Since:
- 0.9.15
-