Class StringUtil2


  • public class StringUtil2
    extends Object
    Static String and StringBuilder utilities
    Since:
    7/29/11
    • Constructor Detail

      • StringUtil2

        public StringUtil2()
    • Method Detail

      • allow

        public static String allow​(String x,
                                   String allowChars,
                                   char replaceChar)
        Replace any char not alphanumeric or in allowChars by replaceChar.
        Parameters:
        x - operate on this string
        allowChars - these are ok.
        replaceChar - thar char to replace
        Returns:
        resulting string.
      • breakTextAtWords

        public static String breakTextAtWords​(String text,
                                              String insert,
                                              int lineSize)
        Break the given text into lines, respecting word boundaries (blank space).
        Parameters:
        text - The text to convert
        insert - break to insert
        lineSize - line size to insert at
        Returns:
        The text with added br tags.
      • cleanup

        public static String cleanup​(byte[] h)
        Delete any non-printable characters
        Parameters:
        h - byte array
        Returns:
        cleaned up string
      • filter

        public static String filter​(String x,
                                    String okChars)
        Remove any char not alphanumeric or in okChars.
        Parameters:
        x - filter this string
        okChars - these are ok.
        Returns:
        filtered string.
      • filter7bits

        public static String filter7bits​(String s)
        Remove all but printable ascii
        Parameters:
        s - filter this string
        Returns:
        filtered string.
      • makeValidCdmObjectName

        public static String makeValidCdmObjectName​(String name)
      • match

        public static int match​(String s1,
                                String s2)
        Count number of chars that match in two strings, starting from front.
        Parameters:
        s1 - compare this string
        s2 - compare this string
        Returns:
        number of matching chars, starting from first char
      • padZero

        public static String padZero​(int value,
                                     int numDigits)
        Left pad the given value with zeros up to the number of digits
        Parameters:
        value - The value.
        numDigits - number of digits
        Returns:
        The String represenation of the value, padded with leading "0"-s if value < 10E(numDigits-1)
      • padLeft

        public static String padLeft​(String s,
                                     int desiredLength)
        Pad the given string with spaces on the left up to the given length.
        Parameters:
        s - String to pad
        desiredLength - ending length
        Returns:
        padded String
      • padLeft

        public static String padLeft​(String s,
                                     int desiredLength,
                                     String padString)
        Pad the given string with padString on the left up to the given length.
        Parameters:
        s - String to pad
        desiredLength - ending length
        padString - String to pad with (e.g, " ")
        Returns:
        padded String
      • padRight

        public static String padRight​(String s,
                                      int desiredLength)
        Pad the given string with spaces on the right up to the given length.
        Parameters:
        s - String to pad
        desiredLength - ending length
        Returns:
        padded String
      • padRight

        public static String padRight​(String s,
                                      int desiredLength,
                                      String padString)
        Pad the given string with padString on the right up to the given length.
        Parameters:
        s - String to pad
        desiredLength - ending length
        padString - String to pad with (e.g, " ")
        Returns:
        padded String
      • remove

        public static String remove​(String s,
                                    String sub)
        Remove all occurrences of the substring sub in the string s.
        Parameters:
        s - operate on this string
        sub - remove all occurrences of this substring.
        Returns:
        result with substrings removed
      • remove

        public static String remove​(String s,
                                    int c)
        Remove all occurrences of the character c in the string s.
        Parameters:
        s - operate on this string
        c - remove all occurrences of this character.
        Returns:
        result with any character c removed
      • removeFromEnd

        public static String removeFromEnd​(String s,
                                           int c)
        Remove all occurrences of the character c at the end of s.
        Parameters:
        s - operate on this string
        c - remove all occurrences of this character that are at the end of the string.
        Returns:
        result with any character c removed
      • removeWhitespace

        public static String removeWhitespace​(String inputString)
        Remove any whitespace (ie., Character.isWhitespace) from the input string.
        Parameters:
        inputString - The string to remove the whitespace.
        Returns:
        The whitespaceless result.
      • collapseWhitespace

        public static String collapseWhitespace​(String s)
        Collapse continuous whitespace into one single " ".
        Parameters:
        s - operate on this string
        Returns:
        result with collapsed whitespace
      • replace

        public static String replace​(String s,
                                     char out,
                                     String in)
        Replace any char "out" in s with "in".
        Parameters:
        s - string to replace
        out - replace this character
        in - with this string
        Returns:
        modified string if needed
      • replace

        public static String replace​(String x,
                                     char[] replaceChar,
                                     String[] replaceWith)
        Replace all occurrences of any char in replaceChar with corresponding String in replaceWith
        Parameters:
        x - operate on this string
        replaceChar - get rid of these
        replaceWith - replace with these
        Returns:
        resulting string
      • replace

        public static String replace​(String string,
                                     String pattern,
                                     String value)
        Replaces all occurrences of "pattern" in "string" with "value"
        Parameters:
        string - string to munge
        pattern - pattern to replace
        value - replacement value
        Returns:
        munged string
      • unreplace

        public static String unreplace​(String x,
                                       String[] orgReplace,
                                       char[] orgChar)
        Replace all occurrences of orgReplace with orgChar; inverse of replace().
        Parameters:
        x - operate on this string
        orgReplace - get rid of these
        orgChar - replace with these
        Returns:
        resulting string
      • substitute

        @Deprecated
        public static String substitute​(String original,
                                        String match,
                                        String subst)
        Deprecated.
        use original.replace(match, subst)
        Find all occurrences of the "match" in original, and substitute the "subst" string.
        Parameters:
        original - starting string
        match - string to match
        subst - string to substitute
        Returns:
        a new string with substitutions
      • escape

        public static String escape​(String x,
                                    String okChars)
        Escape any char not alphanumeric or in okChars. Escape by replacing char with %xx (hex).
        Parameters:
        x - escape this string
        okChars - these are ok.
        Returns:
        equivilent escaped string.
      • unescape

        public static String unescape​(String x)
        This finds any '%xx' and converts to the equivalent char. Inverse of escape().
        Parameters:
        x - operate on this String
        Returns:
        original String.
      • splitString

        @Deprecated
        public static String[] splitString​(String source)
        Deprecated.
        use StringUtil2.split()
        Split a string on one or more whitespace. Cover for String.split, because who can remember regexp?
        Parameters:
        source - split this string
        Returns:
        tokens that were separated by whitespace
      • substitute

        public static String substitute​(String original,
                                        String[] match,
                                        String[] subst)
        Find all occurences of match strings in original, and substitute the corresponding subst string.
        Parameters:
        original - starting string
        match - array of strings to match
        subst - array of strings to substitute
        Returns:
        a new string with substitutions
      • removeAll

        public static void removeAll​(StringBuilder sb,
                                     String out)
        Remove any of the characters in out from sb
        Parameters:
        sb - the StringBuilder
        out - get rid of any of these characters
      • replace

        public static void replace​(StringBuilder sb,
                                   char out,
                                   String in)
        Replace any char "out" in sb with String "in".
        Parameters:
        sb - StringBuilder to replace
        out - repalce this character
        in - with this string
      • unreplace

        public static void unreplace​(StringBuilder sb,
                                     String out,
                                     char in)
        Replace any String "out" in sb with char "in".
        Parameters:
        sb - StringBuilder to replace
        out - repalce this String
        in - with this char
      • replace

        public static void replace​(StringBuilder sb,
                                   String out,
                                   String in)
        Replace any of the characters from out with corresponding character from in
        Parameters:
        sb - the StringBuilder
        out - get rid of any of these characters
        in - replacing with the character at same index
      • substitute

        public static void substitute​(StringBuilder sbuff,
                                      String match,
                                      String subst)
        Find all occurences of the "match" in original, and substitute the "subst" string, directly into the original.
        Parameters:
        sbuff - starting string buffer
        match - string to match
        subst - string to substitute
      • trim

        public static String trim​(String s,
                                  int bad)
        Remove bad char from beginning or end of string
        Parameters:
        s - operate on this
        Returns:
        trimmed string
      • quoteHtmlContent

        public static String quoteHtmlContent​(String x)
        Deprecated.
        legacy only, use HtmlEscapers.htmlEscaper()