com.groiss.util
Class StringUtil

java.lang.Object
  extended by com.groiss.util.StringUtil

public class StringUtil
extends java.lang.Object

Some convenient string manipulation methods.


Method Summary
static java.lang.String decodeURL(java.lang.String url)
           
static java.lang.String encodeURL(java.lang.String url)
          This calls the URLEncoder.encode with the configured charset.
static boolean equalStrings(java.lang.String first, java.lang.String second)
          Checks if both strings are equal.
static boolean escapeForSQL(java.lang.CharSequence txt)
          Escapes all occurencies of character '_' in the passed string builder with the escape character '\'.
static java.lang.String escapeJavaStyleString(java.lang.String str, boolean escapeSingleQuote)
           
static java.lang.String format(java.lang.String pattern, java.lang.Object[] arguments)
          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 java.lang.String getCharset()
           
static java.text.Collator getCollator()
          Returns a Collator to sort Strings correctly
static java.lang.String htmlDecode(java.lang.String s)
          Decodes html entities defined in XHTML 1.0.
static java.lang.String htmlEncode(java.lang.Object o)
          Translates some special characters to their representation in HTML.
static boolean isEmpty(java.lang.String s)
          Is the string null or the empty string
static boolean isNotEmpty(java.lang.String s)
           
static boolean isNumber(java.lang.String s)
           
static boolean isRTL(java.util.Locale loc)
          Is the writing direction in the given locale right-to-left? This is for example true in Hebrew, Arabic, or Persian
static java.lang.String noEmpty(java.lang.Object o)
          Returns " " when argument is null, otherwise o.toString()
static java.lang.String noEmpty(java.lang.String s)
          Returns " " when argument is null, otherwise the string itself.
static java.lang.String noNull(java.lang.Object s)
          Returns the empty string when argument is null, otherwise s.toString()
static java.lang.String noNull(java.lang.String s)
          Returns the empty string when argument is null, otherwise the string itself.
static java.lang.String noTags(java.lang.String text)
          De-HTMLize a string.
static java.lang.String quoteForSQL(java.lang.String arg)
          Doubles the singe quote ' to ''.
static java.lang.String replaceChar(java.lang.String str, char oldch, java.lang.String newStr)
          Replace a character by a string.
static void setCharset(java.lang.String charSet)
           
static java.util.List<java.lang.String> split(java.lang.String mask, java.lang.String pat)
          Split a string into pieces.
static java.util.List<java.lang.String> split(java.lang.String mask, java.lang.String pat, boolean withPat)
          Split a string into pieces.
static java.lang.String substitute(java.lang.String mask, java.lang.String pat, java.lang.String value)
          Substitute a value for a pattern
static java.lang.String[] toArray(java.lang.String s, java.lang.String delim)
          Splits the passed string using the passed delimiter and returns the trimmed parts as array.
static java.lang.String transformForSQL(java.lang.String arg)
          Replaces '*' with '%' and '?' with '_' which are the correct wildcards for sql queries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

substitute

public static java.lang.String substitute(java.lang.String mask,
                                          java.lang.String pat,
                                          java.lang.String value)
Substitute a value for a pattern

Parameters:
mask - the mask containing the patterns
pat - the pattern to substitute
value - the value inserted instead of the pattern
Returns:
the mask with the substitutions

noNull

public static java.lang.String noNull(java.lang.String s)
Returns the empty string when argument is null, otherwise the string itself.

Parameters:
s - a string
Returns:
a string, never null

noNull

public static java.lang.String noNull(java.lang.Object s)
Returns the empty string when argument is null, otherwise s.toString()

Parameters:
s - a object
Returns:
a string, never null

noEmpty

public static java.lang.String noEmpty(java.lang.String s)
Returns "&nbsp;" when argument is null, otherwise the string itself.

Parameters:
s - a string
Returns:
a string, never null or ""

noEmpty

public static java.lang.String noEmpty(java.lang.Object o)
Returns "&nbsp;" when argument is null, otherwise o.toString()

Parameters:
o - a object
Returns:
a string, never null or ""

quoteForSQL

public static java.lang.String quoteForSQL(java.lang.String arg)
Doubles the singe quote ' to ''. For use in sql expressions.

Parameters:
arg - a string
Returns:
a string

transformForSQL

public static java.lang.String transformForSQL(java.lang.String arg)
Replaces '*' with '%' and '?' with '_' which are the correct wildcards for sql queries. Also method quoteForSQL(String) is called

Parameters:
arg - the string in which the replacements and quotes should be performed
Returns:
the sql conform string

escapeForSQL

public static boolean escapeForSQL(java.lang.CharSequence txt)
Escapes all occurencies 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

public static java.lang.String htmlEncode(java.lang.Object o)
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

public static java.lang.String htmlDecode(java.lang.String s)
Decodes html entities defined in XHTML 1.0. Be careful, this method is very inefficient, because it uses the XML Parser to find the entities

Parameters:
s - a string containing entities like &lt;
Returns:
the decoded html entity

replaceChar

public static java.lang.String replaceChar(java.lang.String str,
                                           char oldch,
                                           java.lang.String newStr)
Replace a character by a string.

Parameters:
str - the string
oldch - the character to replace
newStr - the new string
Returns:
the result of replacements

split

public static java.util.List<java.lang.String> split(java.lang.String mask,
                                                     java.lang.String pat)
Split a string into pieces.

Parameters:
mask - the mask to split
pat - the separator for splitting
Returns:
a List containing the pieces

split

public static java.util.List<java.lang.String> split(java.lang.String mask,
                                                     java.lang.String pat,
                                                     boolean withPat)
Split a string into pieces.

Parameters:
mask - the string to split
pat - the separator for splitting
withPat - if true, the strings start with the split pattern
Returns:
the parts

noTags

public static java.lang.String noTags(java.lang.String text)
De-HTMLize a string.

Parameters:
text - a string containing HTML tags
Returns:
the string without HTML tags

equalStrings

public static boolean equalStrings(java.lang.String first,
                                   java.lang.String second)
Checks if both strings are equal. Null is definied to be equal to the empty string within this method.


isEmpty

public static boolean isEmpty(java.lang.String s)
Is the string null or the empty string

Parameters:
s - the string
Returns:
true, if the string is null or the empty string

isNotEmpty

public static boolean isNotEmpty(java.lang.String s)

encodeURL

public static java.lang.String encodeURL(java.lang.String url)
This calls the URLEncoder.encode with the configured charset. It makes an additional transformation: the space char is converted to a "+" by the encoder. Because some decoders (Jetty!) don't recogize this, we transform it to "%20". This is also compatible to decodeURL.

Parameters:
url - the url containing special characters
Returns:
the encoded url

decodeURL

public static java.lang.String decodeURL(java.lang.String url)

getCharset

public static java.lang.String getCharset()

setCharset

public static void setCharset(java.lang.String charSet)

isRTL

public static boolean isRTL(java.util.Locale loc)
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

public static java.lang.String format(java.lang.String pattern,
                                      java.lang.Object[] arguments)
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 format
arguments - the arguments for the pattern
Returns:
the formatted text

isNumber

public static boolean isNumber(java.lang.String s)

escapeJavaStyleString

public static java.lang.String escapeJavaStyleString(java.lang.String str,
                                                     boolean escapeSingleQuote)

getCollator

public static java.text.Collator getCollator()

Returns a Collator to sort Strings correctly

Returns:
the Collator initalized with the ThreadLocale

toArray

public static java.lang.String[] toArray(java.lang.String s,
                                         java.lang.String delim)
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 split
delim - the delimiter for splitting
Returns:
an array holding the trimmed parts or null if no parts could be found


@enterprise 8.0.22989 Copyright 2001-2017 Groiss Informatics GmbH. All Rights Reserved.