com.groiss.util
Class StringUtil

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

public class StringUtil
extends Object

Some convenient string manipulation methods.


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

Method Detail

substitute

public static String substitute(String mask,
                                String pat,
                                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 String noNull(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 String noNull(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 String noEmpty(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 String noEmpty(Object o)
Returns "&nbsp;" when argument is null, otherwise o.toString()

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

quoteForSQL

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

Parameters:
arg - a string
Returns:
a string

transformForSQL

public static String transformForSQL(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(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 String htmlEncode(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 String htmlDecode(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 String replaceChar(String str,
                                 char oldch,
                                 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 List<String> split(String mask,
                                 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 List<String> split(String mask,
                                 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 String noTags(String text)
De-HTMLize a string.

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

equalStrings

public static boolean equalStrings(String first,
                                   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(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(String s)

encodeURL

public static String encodeURL(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 String decodeURL(String url)

getCharset

public static String getCharset()

setCharset

public static void setCharset(String charSet)

isRTL

public static boolean isRTL(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 String format(String pattern,
                            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(String s)

escapeJavaStyleString

public static String escapeJavaStyleString(String str,
                                           boolean escapeSingleQuote)

getCollator

public static Collator getCollator()

Returns a Collator to sort Strings correctly

Returns:
the Collator initalized with the ThreadLocale


@enterprise 8.0.10809 Copyright © 2001-2012 Groiss Informatics GmbH. All Rights Reserved.