Class StringUtils

java.lang.Object
de.iip_ecosphere.platform.support.StringUtils

public class StringUtils extends Object
String utility functions.
Author:
Holger Eichelberger, SSE
  • Constructor Details

    • StringUtils

      public StringUtils()
  • Method Details

    • escapeJava

      public static String escapeJava(String input)
      Escapes the characters in a String using Java String rules. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
      Parameters:
      input - String to escape values in, may be null
      Returns:
      String with escaped values, null if null string input
    • unescapeJava

      public static String unescapeJava(String input)
      Unescapes any Java literals found in the String.
      Parameters:
      input - the String to unescape, may be null
      Returns:
      a new unescaped String, null if null string input
    • escapeJson

      public static String escapeJson(String input)
      Escapes the characters in a String using Json String rules. Escapes any values it finds into their Json String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)
      Parameters:
      input - String to escape values in, may be null
      Returns:
      String with escaped values, null if null string input
    • unescapeJson

      public static String unescapeJson(String input)
      Unescapes any Json literals found in the String.
      Parameters:
      input - the String to unescape, may be null
      Returns:
      A new unescaped String, null if null string input
    • defaultIfBlank

      public static <T extends CharSequence> T defaultIfBlank(T str, T defaultStr)
      Returns either the passed in CharSequence, or if the CharSequence is whitespace, empty ("") or null, the value of defaultStr.
      Type Parameters:
      T - the specific kind of CharSequence
      Parameters:
      str - the CharSequence to check, may be null
      defaultStr - the default CharSequence to return if the input is whitespace, empty ("") or null
      Returns:
      the passed in CharSequence, or the default
    • defaultIfEmpty

      public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr)
      Returns either the passed in CharSequence, or if the CharSequence is empty or null, the value of defaultStr.
      Type Parameters:
      T - the specific kind of CharSequence
      Parameters:
      str - the CharSequence to check, may be null
      defaultStr - the default CharSequence to return if the input is empty ("") or null
      Returns:
      the passed in CharSequence, or the default
    • length

      public static int length(CharSequence cs)
      Gets a CharSequence length or 0 if the CharSequence is null.
      Parameters:
      cs - a CharSequence, may be null
      Returns:
      CharSequence length or 0 if the CharSequence is null.
    • isBlank

      public static boolean isBlank(CharSequence cs)
      Checks if a CharSequence is empty (""), null or whitespace only.
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is null, empty or whitespace only
    • isNotBlank

      public static boolean isNotBlank(CharSequence cs)
      Checks if a CharSequence is not empty (""), not null and not whitespace only.
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is not empty and not null and not whitespace only
    • replaceOnce

      public static String replaceOnce(String text, String searchString, String replacement)
      Replaces a String with another String inside a larger String, once.
      Parameters:
      text - text to search and replace in, may be null
      searchString - the String to search for, may be null
      replacement - the String to replace with, may be null
      Returns:
      the text with any replacements processed, null if null String input
    • isEmpty

      public static boolean isEmpty(CharSequence cs)
      Checks if a CharSequence is empty ("") or null.
      Parameters:
      cs - the CharSequence to check, may be null
      Returns:
      true if the CharSequence is empty or null
    • toString

      public static String toString(Object obj)
      Turns an object to an readable string, usually using reflection. Uses some default style.
      Parameters:
      obj - the object
      Returns:
      the string representation
    • toStringShortStyle

      public static String toStringShortStyle(Object obj)
      Turns an object to an readable string, usually using reflection. Uses oktoflow short style.
      Parameters:
      obj - the object
      Returns:
      the string representation
    • removeStart

      public static String removeStart(String str, String remove)
      Removes a substring only if it is at the beginning of a source string, otherwise returns the source string.
      Parameters:
      str - the source String to search, may be null
      remove - the String to search for and remove, may be null
      Returns:
      the substring with the string removed if found, null if null String input
    • removeEnd

      public static String removeEnd(String str, String remove)
      Removes a substring only if it is at the end of a source string, otherwise returns the source string.
      Parameters:
      str - the source String to search, may be null
      remove - the String to search for and remove, may be null
      Returns:
      the substring with the string removed if found, null if null String input
    • toTokenList

      public static List<String> toTokenList(StringTokenizer tokenizer)
      Turns the tokens of the given tokenizer into a list.
      Parameters:
      tokenizer - the tokenizer
      Returns:
      the list
    • toArray

      public static String[] toArray(List<String> list)
      Turns a string list to an array.
      Parameters:
      list - the list, may be null
      Returns:
      the corresponding array, also null if list is null
    • toTokenArray

      public static String[] toTokenArray(StringTokenizer tokenizer)
      Turns the tokens of the given tokenizer into an array.
      Parameters:
      tokenizer - the tokenizer
      Returns:
      the array