Class StringUtil


  • public 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.StringBuilder append​(java.lang.StringBuilder sb, java.lang.Iterable<? extends java.lang.Object> iterable, java.lang.String prefix, java.lang.String suffix, java.lang.String sep)  
      static java.lang.StringBuilder append​(java.lang.StringBuilder sb, java.lang.Iterable<? extends java.lang.Object> iterable, java.lang.String prefix, java.lang.String suffix, java.lang.String sepPrefix, java.lang.String sep, java.lang.String sepSuffix)  
      static java.lang.StringBuilder appendTo​(java.lang.StringBuilder sb, java.lang.Iterable<? extends Appender<java.lang.StringBuilder>> iterable, java.lang.String prefix, java.lang.String suffix, java.lang.String sep)  
      static java.lang.StringBuilder appendTo​(java.lang.StringBuilder sb, java.lang.Iterable<? extends Appender<java.lang.StringBuilder>> iterable, java.lang.String prefix, java.lang.String suffix, java.lang.String sepPrefix, java.lang.String sep, java.lang.String sepSuffix)  
      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 collapseCharacter​(java.lang.String str, char c)
      Remove/collapse multiple characters
      static java.lang.String collapseNewlines​(java.lang.String str)
      Remove/collapse multiple newline characters
      static java.lang.String collapseSpaces​(java.lang.String str)
      Remove/collapse multiple spaces
      static int convertChar​(char c)  
      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.String delim)
      Creates a single string from a Collection of strings seperated by a delimiter.
      static java.lang.String join​(java.util.List<?> list, java.lang.String delim)
      Creates a single string from a List of strings seperated by a delimiter.
      static StringUtil.StringWrapper makeStringWrapper​(java.lang.String theString)  
      static java.lang.String mapToStr​(java.util.Map<? extends java.lang.Object,​? extends java.lang.Object> map)
      Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)
      static java.lang.String padNumberString​(java.lang.String numberString, int targetMinLength)  
      static java.util.List<java.lang.String> quoteStrList​(java.util.List<java.lang.String> list)
      Encloses each of a List of Strings in quotes.
      static java.lang.String removeNonNumeric​(java.lang.String str)
      Removes all non-numbers from str
      static java.lang.String removeNumeric​(java.lang.String str)
      Removes all 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.List<java.lang.String> split​(java.lang.String str, java.lang.String delim, int limit)
      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)
      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.util.Map<java.lang.String,​java.lang.String> strToMap​(java.lang.String str, java.lang.String delim, boolean trim, java.lang.String pairsSeparator)
      Creates a Map from a 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.Map<java.lang.String,​java.lang.String> toMap​(java.lang.String s)
      Reads a String version of a Map (should contain only strings) and creates a new Map.
      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
      • module

        public static final java.lang.String module
    • 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.List<?> list,
                                            java.lang.String delim)
        Creates a single string from a List of strings seperated by a delimiter.
        Parameters:
        list - a list 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 list seperated by the delimiter
      • join

        public static java.lang.String join​(java.util.Collection<?> col,
                                            java.lang.String delim)
        Creates a single string from a Collection of strings seperated 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 seperated 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
      • split

        public static java.util.List<java.lang.String> split​(java.lang.String str,
                                                             java.lang.String delim,
                                                             int limit)
        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)
        limit - see String.split() method
        Returns:
        a list of Strings
      • quoteStrList

        public static java.util.List<java.lang.String> quoteStrList​(java.util.List<java.lang.String> list)
        Encloses each of a List of Strings in quotes.
        Parameters:
        list - List of String(s) to quote.
      • 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,
                                                                                      java.lang.String delim,
                                                                                      boolean trim,
                                                                                      java.lang.String pairsSeparator)
        Creates a Map from a 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
        pairsSeparator - in case you use not encoded name/value pairs strings and want to replace "=" to avoid clashes with parameters values in a not encoded URL, default to "="
        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,
                                                                                      java.lang.String delim)
        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)
        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
      • mapToStr

        public static java.lang.String mapToStr​(java.util.Map<? extends java.lang.Object,​? extends java.lang.Object> map)
        Creates an encoded String from a Map of name/value pairs (MUST BE STRINGS!)
        Parameters:
        map - The Map of name/value pairs
        Returns:
        String The encoded String
      • toMap

        public static java.util.Map<java.lang.String,​java.lang.String> toMap​(java.lang.String s)
        Reads a String version of a Map (should contain only strings) and creates a new Map. Partial Map elements are skipped: {foo=fooValue, bar=} will contain only the foo element.
        Parameters:
        s - String value of a Map ({n1=v1, n2=v2})
        Returns:
        new Map
      • 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)
      • convertChar

        public static int convertChar​(char c)
      • 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
      • removeNumeric

        public static java.lang.String removeNumeric​(java.lang.String str)
        Removes all 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
      • collapseNewlines

        public static java.lang.String collapseNewlines​(java.lang.String str)
        Remove/collapse multiple newline characters
        Parameters:
        str - string to collapse newlines in
        Returns:
        the converted string
      • collapseSpaces

        public static java.lang.String collapseSpaces​(java.lang.String str)
        Remove/collapse multiple spaces
        Parameters:
        str - string to collapse spaces in
        Returns:
        the converted string
      • collapseCharacter

        public static java.lang.String collapseCharacter​(java.lang.String str,
                                                         char c)
        Remove/collapse multiple characters
        Parameters:
        str - string to collapse characters in
        c - character to collapse
        Returns:
        the converted string
      • appendTo

        public static java.lang.StringBuilder appendTo​(java.lang.StringBuilder sb,
                                                       java.lang.Iterable<? extends Appender<java.lang.StringBuilder>> iterable,
                                                       java.lang.String prefix,
                                                       java.lang.String suffix,
                                                       java.lang.String sep)
      • appendTo

        public static java.lang.StringBuilder appendTo​(java.lang.StringBuilder sb,
                                                       java.lang.Iterable<? extends Appender<java.lang.StringBuilder>> iterable,
                                                       java.lang.String prefix,
                                                       java.lang.String suffix,
                                                       java.lang.String sepPrefix,
                                                       java.lang.String sep,
                                                       java.lang.String sepSuffix)
      • append

        public static java.lang.StringBuilder append​(java.lang.StringBuilder sb,
                                                     java.lang.Iterable<? extends java.lang.Object> iterable,
                                                     java.lang.String prefix,
                                                     java.lang.String suffix,
                                                     java.lang.String sep)
      • append

        public static java.lang.StringBuilder append​(java.lang.StringBuilder sb,
                                                     java.lang.Iterable<? extends java.lang.Object> iterable,
                                                     java.lang.String prefix,
                                                     java.lang.String suffix,
                                                     java.lang.String sepPrefix,
                                                     java.lang.String sep,
                                                     java.lang.String sepSuffix)