Package com.groiss.util
Class StringUtil
java.lang.Object
com.groiss.util.StringUtil
Some convenient string manipulation methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
This method breaks a text (i.e. inserts "\n" characters) so that none of its lines exceed the maximum line length specified by the parametermaxl
.static String
static String
This calls the URLEncoder.encode with the UTF-8 charset.static boolean
equalStrings
(String first, String second) Checks if both strings are equal.static boolean
escapeForSQL
(CharSequence txt) Escapes all occurrences of character '_' in the passed string builder with the escape character '\'.static String
escapeJavaStyleString
(String str, boolean escapeSingleQuote) static String
Formats the passed pattern with the passed argument (as known by java.text.MessageFormat(String, Object []) but it guarantees that single quotes remain in the pattern (class MessageFormat would remove them).static String
Deprecated, for removal: This API element is subject to removal in a future version.static Collator
Returns aCollator
to sortStrings
correctlystatic String
getJarPrefix
(String url) Return the jar prefix of the given url string.static String
htmlDecode
(String s) Decodes html entities defined in XHTML 4.0.static String
htmlEncode
(Object o) Translates some special characters to their representation in HTML.static boolean
Is the string null or the empty stringstatic boolean
Determine whether the given url string points to a resource in a jar file.static boolean
isNotEmpty
(String s) static boolean
static boolean
Is the writing direction in the given locale right-to-left?static String
Returns " " when argument is null, otherwise o.toString()static String
Returns " " when argument is null, otherwise the string itself.static String
Returns the empty string when argument is null, otherwise s.toString()static String
Returns the empty string when argument is null, otherwise the string itself.static String
normalizeUnicode
(String str) Normalize to "Canonical decomposition, followed by canonical composition."static String
De-HTMLize a string.static String
quoteForSQL
(String arg) Doubles the single quote ' to two of them ''.static String
replaceChar
(String str, char oldch, String newStr) Replace a character by a string.Split a string into pieces.Split a string into pieces.static String
substitute
(String mask, String pat, String value) Substitute a value for a patternstatic String[]
Splits the passed string using the passed delimiter and returns the trimmed parts as array.static Locale
Get a Locale from a string.static String
transformForSQL
(String arg) Replaces '*' with '%' and '?'
-
Method Details
-
substitute
Substitute a value for a pattern- Parameters:
mask
- the mask containing the patternspat
- the pattern to substitutevalue
- the value inserted instead of the pattern- Returns:
- the mask with the substitutions
-
noNull
Returns the empty string when argument is null, otherwise the string itself.- Parameters:
s
- a string- Returns:
- a string, never null
-
noNull
Returns the empty string when argument is null, otherwise s.toString()- Parameters:
s
- a object- Returns:
- a string, never null
-
noEmpty
Returns " " when argument is null, otherwise the string itself.- Parameters:
s
- a string- Returns:
- a string, never null or ""
-
noEmpty
Returns " " when argument is null, otherwise o.toString()- Parameters:
o
- a object- Returns:
- a string, never null or ""
-
quoteForSQL
Doubles the single quote ' to two of them ''. For use in sql expressions.- Parameters:
arg
- a string- Returns:
- a string
-
transformForSQL
Replaces '*' with '%' and '?' with '_' which are the correct wildcards for sql queries. Also methodquoteForSQL(String)
is called- Parameters:
arg
- the string in which the replacements and quotes should be performed- Returns:
- the sql conform string
-
escapeForSQL
Escapes all occurrences of character '_' in the passed string builder with the escape character '\'. If the escape character itself is an element of arg it will also be escaped.- Parameters:
txt
- holds the string which should be escaped- Returns:
- true if something had to be escaped, false otherwise
-
htmlEncode
Translates some special characters to their representation in HTML.- Parameters:
o
- an object with a toString method, the string is translated- Returns:
- the HTML string
-
htmlDecode
Decodes html entities defined in XHTML 4.0.- Parameters:
s
- a string containing entities like <- Returns:
- the decoded html entity
-
replaceChar
Replace a character by a string.- Parameters:
str
- the stringoldch
- the character to replacenewStr
- the new string- Returns:
- the result of replacements
-
split
Split a string into pieces.- Parameters:
mask
- the mask to splitpat
- the separator for splitting- Returns:
- a List containing the pieces
-
split
Split a string into pieces.- Parameters:
mask
- the string to splitpat
- the separator for splittingwithPat
- if true, the strings start with the split pattern- Returns:
- the parts
-
noTags
De-HTMLize a string.- Parameters:
text
- a string containing HTML tags- Returns:
- the string without HTML tags
-
equalStrings
Checks if both strings are equal. Null is defined to be equal to the empty string within this method. -
isEmpty
Is the string null or the empty string- Parameters:
s
- the string- Returns:
- true, if the string is null or the empty string
-
isNotEmpty
-
encodeURL
This calls the URLEncoder.encode with the UTF-8 charset. It makes an additional transformation: the space char is converted to a "+" by the encoder. Because some decoders (Jetty!) don't recognize this, we transform it to "%20". This is also compatible to decodeURL.- Parameters:
url
- the url containing special characters- Returns:
- the encoded url
-
decodeURL
-
getCharset
Deprecated, for removal: This API element is subject to removal in a future version.UseStandardCharsets.UTF_8.name()
instead.The charset used in communication with the browser. Returns the result of StandardCharsets.UTF_8.name(), which should be "UTF-8".- Returns:
- the name of the standard charset
-
isRTL
Is the writing direction in the given locale right-to-left? This is for example true in Hebrew, Arabic, or Persian- Parameters:
loc
- the locale- Returns:
- boolean true, if right to left
-
format
Formats the passed pattern with the passed argument (as known by java.text.MessageFormat(String, Object []) but it guarantees that single quotes remain in the pattern (class MessageFormat would remove them).- Parameters:
pattern
- the pattern to formatarguments
- the arguments for the pattern- Returns:
- the formatted text
-
isNumber
-
escapeJavaStyleString
-
getCollator
Returns a
Collator
to sortStrings
correctly- Returns:
- the Collator initialized with the
ThreadLocale
-
breakText
This method breaks a text (i.e. inserts "\n" characters) so that none of its lines exceed the maximum line length specified by the parametermaxl
.- Parameters:
text
- the text to breakmaxl
- the maximum line lengthinsertSplitMark
- if true insert the character » where line has been split- Returns:
- the broken text
-
toArray
Splits the passed string using the passed delimiter and returns the trimmed parts as array. Note: if a part contains only blanks it will not be added to the result- Parameters:
s
- the string to be splitdelim
- the delimiter for splitting- Returns:
- an array holding the trimmed parts or null if no parts could be found
-
normalizeUnicode
Normalize to "Canonical decomposition, followed by canonical composition." (NFC). -
toLocale
Get a Locale from a string. If the string is empty, the default locale is returned.- Parameters:
localeString
- the String to convert to a locale- Returns:
- the locale constructed from the string
-
getJarPrefix
Return the jar prefix of the given url string.- Parameters:
url
- a String representation of the url- Returns:
- the jarPrefix, if
isJar(String)
returns true, null else.
-
isJar
Determine whether the given url string points to a resource in a jar file. That is, if it starts with "jar:" (in jdk), "zip:" (in WebLogic), "wsjar:" (in WebSphere) or "vfszip: (JBoss); optionally followed by "file:".- Parameters:
url
- a String representation of the url- Returns:
- true, if the url starts with a known archive protocol
-
StandardCharsets.UTF_8.name()
instead.