Class StringUtil

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

public class StringUtil extends Object
Some convenient string manipulation methods.
  • Method Details

    • 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 " " 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 " " 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 single quote ' to two of them ''. 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 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

      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 4.0.
      Parameters:
      s - a string containing entities like <
      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 defined 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 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

      public static String decodeURL(String url)
    • getCharset

      @Deprecated(since="11.0", forRemoval=true) public static String getCharset()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use StandardCharsets.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

      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 initialized with the ThreadLocale
    • breakText

      public static String breakText(String text, int maxl, boolean insertSplitMark)
      This method breaks a text (i.e. inserts "\n" characters) so that none of its lines exceed the maximum line length specified by the parameter maxl.
      Parameters:
      text - the text to break
      maxl - the maximum line length
      insertSplitMark - if true insert the character » where line has been split
      Returns:
      the broken text
    • toArray

      public static String[] toArray(String s, 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
    • normalizeUnicode

      public static String normalizeUnicode(String str)
      Normalize to "Canonical decomposition, followed by canonical composition." (NFC).
    • toLocale

      public static Locale toLocale(String localeString)
      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

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

      public static boolean isJar(String url)
      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