Class StringUtil


  • public final class StringUtil
    extends java.lang.Object
    Misc String Utility Functions
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  StringUtil.StringWrapper
      A super-lightweight object to wrap a String object.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String addToNumberString​(java.lang.String numberString, long addAmount)
      Add the number to the string, keeping (padding to min of original length)
      static java.lang.String cleanHexString​(java.lang.String str)  
      static java.lang.String cleanUpPathPrefix​(java.lang.String prefix)
      Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string.
      static java.lang.String convertOperatorSubstitutions​(java.lang.String expression)
      Converts operator substitutions (@and, @or, etc) back to their original form.
      static <K,​V>
      java.util.Map<K,​V>
      createMap​(java.util.List<K> keys, java.util.List<V> values)
      Create a Map from a List of keys and a List of values
      static char[] encodeInt​(int i, int j, char[] digestChars)  
      static byte[] fromHexString​(java.lang.String str)  
      static java.lang.String internString​(java.lang.String value)  
      static java.lang.String join​(java.util.Collection<?> col, java.lang.CharSequence delim)
      Creates a single string from a Collection of strings separated by a delimiter.
      static StringUtil.StringWrapper makeStringWrapper​(java.lang.String theString)  
      static java.lang.String padNumberString​(java.lang.String numberString, int targetMinLength)  
      static java.lang.String removeNonNumeric​(java.lang.String str)
      Removes all non-numbers from str
      static java.lang.String removeRegex​(java.lang.String str, java.lang.String regex)  
      static java.lang.String removeSpaces​(java.lang.String str)
      Removes all spaces from a string
      static java.lang.String replaceString​(java.lang.String mainString, java.lang.String oldString, java.lang.String newString)
      Replaces all occurrences of oldString in mainString with newString
      static java.util.List<java.lang.String> split​(java.lang.String str, java.lang.String delim)
      Splits a String on a delimiter into a List of Strings.
      static java.util.Map<java.lang.String,​java.lang.String> strToMap​(java.lang.String str)
      Creates a Map from an encoded name/value pair string
      static java.util.Map<java.lang.String,​java.lang.String> strToMap​(java.lang.String str, boolean trim)
      Creates a Map from an encoded name/value pair string
      static java.util.Map<java.lang.String,​java.lang.String> strToMap​(java.lang.String str, java.lang.String delim, boolean trim)
      Creates a Map from an encoded name/value pair string
      static java.lang.String toHexString​(byte[] bytes)  
      static java.util.List<java.lang.String> toList​(java.lang.String s)
      Reads a String version of a List (should contain only strings) and creates a new List
      static java.util.Set<java.lang.String> toSet​(java.lang.String s)
      Reads a String version of a Set (should contain only strings) and creates a new Set
      static StringUtil.StringWrapper wrapString​(java.lang.String theString)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INSTANCE

        public static final StringUtil INSTANCE
    • Method Detail

      • internString

        public static java.lang.String internString​(java.lang.String value)
      • replaceString

        public static java.lang.String replaceString​(java.lang.String mainString,
                                                     java.lang.String oldString,
                                                     java.lang.String newString)
        Replaces all occurrences of oldString in mainString with newString
        Parameters:
        mainString - The original string
        oldString - The string to replace
        newString - The string to insert in place of the old
        Returns:
        mainString with all occurrences of oldString replaced by newString
      • join

        public static java.lang.String join​(java.util.Collection<?> col,
                                            java.lang.CharSequence delim)
        Creates a single string from a Collection of strings separated by a delimiter.
        Parameters:
        col - a collection of strings to join
        delim - the delimiter character(s) to use. (null value will join with no delimiter)
        Returns:
        a String of all values in the collection separated by the delimiter
      • split

        public static java.util.List<java.lang.String> split​(java.lang.String str,
                                                             java.lang.String delim)
        Splits a String on a delimiter into a List of Strings.
        Parameters:
        str - the String to split
        delim - the delimiter character(s) to join on (null will split on whitespace)
        Returns:
        a list of Strings
      • strToMap

        public static java.util.Map<java.lang.String,​java.lang.String> strToMap​(java.lang.String str,
                                                                                      java.lang.String delim,
                                                                                      boolean trim)
        Creates a Map from an encoded name/value pair string
        Parameters:
        str - The string to decode and format
        delim - the delimiter character(s) to join on (null will split on whitespace)
        trim - Trim whitespace off fields
        Returns:
        a Map of name/value pairs
      • strToMap

        public static java.util.Map<java.lang.String,​java.lang.String> strToMap​(java.lang.String str,
                                                                                      boolean trim)
        Creates a Map from an encoded name/value pair string
        Parameters:
        str - The string to decode and format
        trim - Trim whitespace off fields
        Returns:
        a Map of name/value pairs
      • strToMap

        public static java.util.Map<java.lang.String,​java.lang.String> strToMap​(java.lang.String str)
        Creates a Map from an encoded name/value pair string
        Parameters:
        str - The string to decode and format
        Returns:
        a Map of name/value pairs
      • toList

        public static java.util.List<java.lang.String> toList​(java.lang.String s)
        Reads a String version of a List (should contain only strings) and creates a new List
        Parameters:
        s - String value of a Map ({n1=v1, n2=v2})
        Returns:
        new List
      • toSet

        public static java.util.Set<java.lang.String> toSet​(java.lang.String s)
        Reads a String version of a Set (should contain only strings) and creates a new Set
        Parameters:
        s - String value of a Map ({n1=v1, n2=v2})
        Returns:
        new List
      • createMap

        public static <K,​V> java.util.Map<K,​V> createMap​(java.util.List<K> keys,
                                                                     java.util.List<V> values)
        Create a Map from a List of keys and a List of values
        Parameters:
        keys - List of keys
        values - List of values
        Returns:
        Map of combined lists
        Throws:
        java.lang.IllegalArgumentException - When either List is null or the sizes do not equal
      • cleanUpPathPrefix

        public static java.lang.String cleanUpPathPrefix​(java.lang.String prefix)
        Make sure the string starts with a forward slash but does not end with one; converts back-slashes to forward-slashes; if in String is null or empty, returns zero length string.
      • removeSpaces

        public static java.lang.String removeSpaces​(java.lang.String str)
        Removes all spaces from a string
      • toHexString

        public static java.lang.String toHexString​(byte[] bytes)
      • cleanHexString

        public static java.lang.String cleanHexString​(java.lang.String str)
      • fromHexString

        public static byte[] fromHexString​(java.lang.String str)
      • encodeInt

        public static char[] encodeInt​(int i,
                                       int j,
                                       char[] digestChars)
      • removeNonNumeric

        public static java.lang.String removeNonNumeric​(java.lang.String str)
        Removes all non-numbers from str
      • removeRegex

        public static java.lang.String removeRegex​(java.lang.String str,
                                                   java.lang.String regex)
        Parameters:
        str -
        regex - Removes all matches of regex from a str
      • addToNumberString

        public static java.lang.String addToNumberString​(java.lang.String numberString,
                                                         long addAmount)
        Add the number to the string, keeping (padding to min of original length)
        Returns:
        the new value
      • padNumberString

        public static java.lang.String padNumberString​(java.lang.String numberString,
                                                       int targetMinLength)
      • convertOperatorSubstitutions

        public static java.lang.String convertOperatorSubstitutions​(java.lang.String expression)
        Converts operator substitutions (@and, @or, etc) back to their original form.

        OFBiz script syntax provides special forms of common operators to make it easier to embed logical expressions in XML

        OFBiz XML operators
        OFBiz operatorSubstitution
        @and&&
        @or||
        @gt>
        @gteq>=
        @lt<
        @lteq<=
        Parameters:
        expression - The String to convert
        Returns:
        The converted String