Class StringUtil


  • public final class StringUtil
    extends java.lang.Object
    A number of String-specific utility methods for use by PMD or its IDE plugins.
    Author:
    BrianRemedios
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void appendXmlEscaped​(java.lang.StringBuilder buf, java.lang.String src, boolean supportUTF8)  
      static boolean areSemanticEquals​(java.lang.String a, java.lang.String b)
      Deprecated.
      static java.lang.String asString​(java.lang.Object[] items, java.lang.String separator)
      Formats all items onto a string with separators if more than one exists, return an empty string if the items are null or empty.
      static void asStringOn​(java.lang.StringBuffer sb, java.util.Iterator<?> iter, java.lang.String separator)
      Deprecated.
      StringUtils.join(Iterator, String)
      static void asStringOn​(java.lang.StringBuilder sb, java.lang.Object[] items, java.lang.String separator)
      Deprecated.
      StringUtils.join(Iterable, String)
      static java.lang.String escapeWhitespace​(java.lang.Object o)
      Replace some whitespace characters so they are visually apparent.
      static java.lang.String[] getEmptyStrings()
      Returns an empty array of string
      static boolean isAnyOf​(java.lang.String text, java.lang.String... tests)
      Returns whether the non-null text arg matches any of the test values.
      static boolean isEmpty​(java.lang.String value)
      Deprecated.
      StringUtils.isBlank(CharSequence)
      static boolean isMissing​(java.lang.String value)
      Deprecated.
      StringUtils.isEmpty(CharSequence)
      static boolean isNotEmpty​(java.lang.String value)
      Deprecated.
      StringUtils.isNotBlank(CharSequence)
      static boolean isSame​(java.lang.String s1, java.lang.String s2, boolean trim, boolean ignoreCase, boolean standardizeWhitespace)
      Are the two String values the same.
      static int lengthOfShortestIn​(java.lang.String[] strings)
      Return the length of the shortest string in the array.
      static java.lang.String lpad​(java.lang.String s, int length)
      Deprecated.
      StringUtils.leftPad(String, int)
      static int maxCommonLeadingWhitespaceForAll​(java.lang.String[] strings)
      Determine the maximum number of common leading whitespace characters the strings share in the same sequence.
      static java.lang.String percentageString​(double val, int numDecimals)
      Formats a double to a percentage, keeping numDecimal decimal places.
      static java.lang.String replaceString​(java.lang.String original, char oldChar, java.lang.String newString)
      Deprecated.
      StringUtils.replace(String, String, String) or StringUtils.replaceChars(String, char, char)
      static java.lang.String replaceString​(java.lang.String original, java.lang.String oldString, java.lang.String newString)
      Deprecated.
      StringUtils.replace(String, String, String)
      static boolean startsWithAny​(java.lang.String text, java.lang.String... prefixes)
      Deprecated.
      StringUtils.startsWithAny(CharSequence, CharSequence...)
      static java.lang.String[] substringsOf​(java.lang.String source, char delimiter)
      Deprecated.
      StringUtils.split(String, char)
      static java.lang.String[] substringsOf​(java.lang.String str, java.lang.String separator)
      Deprecated.
      StringUtils.split(String, String)
      static java.lang.String toCamelCase​(java.lang.String name)
      Converts the given string to Camel case, that is, removing all spaces, and capitalising the first letter of each word except the first.
      static java.lang.String toCamelCase​(java.lang.String name, boolean forceLowerCase)
      Converts the given string to Camel case, that is, removing all spaces, and capitalising the first letter of each word except the first.
      static java.lang.String[] trimStartOn​(java.lang.String[] strings, int trimDepth)
      Trims off the leading characters off the strings up to the trimDepth specified.
      static java.lang.String withoutPrefixes​(java.lang.String text, java.lang.String... prefixes)
      Checks for the existence of any of the listed prefixes on the non-null text and removes them.
      • Methods inherited from class java.lang.Object

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

      • percentageString

        public static java.lang.String percentageString​(double val,
                                                        int numDecimals)
        Formats a double to a percentage, keeping numDecimal decimal places.
        Parameters:
        val - a double value between 0 and 1
        numDecimals - The number of decimal places to keep
        Returns:
        A formatted string
        Throws:
        java.lang.IllegalArgumentException - if the double to format is not between 0 and 1
      • startsWithAny

        @Deprecated
        public static boolean startsWithAny​(java.lang.String text,
                                            java.lang.String... prefixes)
        Deprecated.
        StringUtils.startsWithAny(CharSequence, CharSequence...)
        Return whether the non-null text arg starts with any of the prefix values.
        Parameters:
        text -
        prefixes -
        Returns:
        boolean
      • isAnyOf

        public static boolean isAnyOf​(java.lang.String text,
                                      java.lang.String... tests)
        Returns whether the non-null text arg matches any of the test values.
        Parameters:
        text -
        tests -
        Returns:
        boolean
      • withoutPrefixes

        public static java.lang.String withoutPrefixes​(java.lang.String text,
                                                       java.lang.String... prefixes)
        Checks for the existence of any of the listed prefixes on the non-null text and removes them.
        Parameters:
        text -
        prefixes -
        Returns:
        String
      • isNotEmpty

        @Deprecated
        public static boolean isNotEmpty​(java.lang.String value)
        Deprecated.
        StringUtils.isNotBlank(CharSequence)
        Parameters:
        value - String
        Returns:
        boolean
      • isEmpty

        @Deprecated
        public static boolean isEmpty​(java.lang.String value)
        Deprecated.
        StringUtils.isBlank(CharSequence)
        Returns true if the value arg is either null, empty, or full of whitespace characters. More efficient that calling (string).trim().length() == 0.
        Parameters:
        value - String to test
        Returns:
        true if the value is empty, false otherwise.
      • isMissing

        @Deprecated
        public static boolean isMissing​(java.lang.String value)
        Deprecated.
        StringUtils.isEmpty(CharSequence)
        Returns true if the argument is null or the empty string.
        Parameters:
        value - String to test
        Returns:
        True if the argument is null or the empty string
      • areSemanticEquals

        @Deprecated
        public static boolean areSemanticEquals​(java.lang.String a,
                                                java.lang.String b)
        Deprecated.
        Returns true if both strings are effectively null or whitespace, returns false otherwise if they have actual text that differs.
        Parameters:
        a -
        b -
        Returns:
        boolean
      • replaceString

        @Deprecated
        public static java.lang.String replaceString​(java.lang.String original,
                                                     java.lang.String oldString,
                                                     java.lang.String newString)
        Deprecated.
        StringUtils.replace(String, String, String)
        Parameters:
        original - String
        oldString - String
        newString - String
        Returns:
        String
      • appendXmlEscaped

        public static void appendXmlEscaped​(java.lang.StringBuilder buf,
                                            java.lang.String src,
                                            boolean supportUTF8)
        Parameters:
        buf -
        src -
        supportUTF8 - override the default setting, whether special characters should be replaced with entities ( false) or should be included as is ( true).
      • escapeWhitespace

        public static java.lang.String escapeWhitespace​(java.lang.Object o)
        Replace some whitespace characters so they are visually apparent.
        Parameters:
        o -
        Returns:
        String
      • replaceString

        @Deprecated
        public static java.lang.String replaceString​(java.lang.String original,
                                                     char oldChar,
                                                     java.lang.String newString)
        Deprecated.
        StringUtils.replace(String, String, String) or StringUtils.replaceChars(String, char, char)
        Parameters:
        original - String
        oldChar - char
        newString - String
        Returns:
        String
      • substringsOf

        @Deprecated
        public static java.lang.String[] substringsOf​(java.lang.String source,
                                                      char delimiter)
        Deprecated.
        StringUtils.split(String, char)
        Parses the input source using the delimiter specified. This method is much faster than using the StringTokenizer or String.split(char) approach and serves as a replacement for String.split() for JDK1.3 that doesn't have it.
        Parameters:
        source - String
        delimiter - char
        Returns:
        String[]
      • substringsOf

        @Deprecated
        public static java.lang.String[] substringsOf​(java.lang.String str,
                                                      java.lang.String separator)
        Deprecated.
        StringUtils.split(String, String)
        Much more efficient than StringTokenizer.
        Parameters:
        str - String
        separator - char
        Returns:
        String[]
      • asStringOn

        @Deprecated
        public static void asStringOn​(java.lang.StringBuffer sb,
                                      java.util.Iterator<?> iter,
                                      java.lang.String separator)
        Deprecated.
        StringUtils.join(Iterator, String)
        Copies the elements returned by the iterator onto the string buffer each delimited by the separator.
        Parameters:
        sb - StringBuffer
        iter - Iterator
        separator - String
      • asStringOn

        @Deprecated
        public static void asStringOn​(java.lang.StringBuilder sb,
                                      java.lang.Object[] items,
                                      java.lang.String separator)
        Deprecated.
        StringUtils.join(Iterable, String)
        Copies the array items onto the string builder each delimited by the separator. Does nothing if the array is null or empty.
        Parameters:
        sb - StringBuilder
        items - Object[]
        separator - String
      • maxCommonLeadingWhitespaceForAll

        public static int maxCommonLeadingWhitespaceForAll​(java.lang.String[] strings)
        Determine the maximum number of common leading whitespace characters the strings share in the same sequence. Useful for determining how many leading characters can be removed to shift all the text in the strings to the left without misaligning them.
        Parameters:
        strings - String[]
        Returns:
        int
      • lengthOfShortestIn

        public static int lengthOfShortestIn​(java.lang.String[] strings)
        Return the length of the shortest string in the array. If the collection is empty or any one of them is null then it returns 0.
        Parameters:
        strings - String[]
        Returns:
        int
      • trimStartOn

        public static java.lang.String[] trimStartOn​(java.lang.String[] strings,
                                                     int trimDepth)
        Trims off the leading characters off the strings up to the trimDepth specified. Returns the same strings if trimDepth = 0
        Parameters:
        strings -
        trimDepth -
        Returns:
        String[]
      • lpad

        @Deprecated
        public static java.lang.String lpad​(java.lang.String s,
                                            int length)
        Deprecated.
        StringUtils.leftPad(String, int)
        Left pads a string.
        Parameters:
        s - The String to pad
        length - The desired minimum length of the resulting padded String
        Returns:
        The resulting left padded String
      • isSame

        public static boolean isSame​(java.lang.String s1,
                                     java.lang.String s2,
                                     boolean trim,
                                     boolean ignoreCase,
                                     boolean standardizeWhitespace)
        Are the two String values the same. The Strings can be optionally trimmed before checking. The Strings can be optionally compared ignoring case. The Strings can be have embedded whitespace standardized before comparing. Two null values are treated as equal.
        Parameters:
        s1 - The first String.
        s2 - The second String.
        trim - Indicates if the Strings should be trimmed before comparison.
        ignoreCase - Indicates if the case of the Strings should ignored during comparison.
        standardizeWhitespace - Indicates if the embedded whitespace should be standardized before comparison.
        Returns:
        true if the Strings are the same, false otherwise.
      • asString

        public static java.lang.String asString​(java.lang.Object[] items,
                                                java.lang.String separator)
        Formats all items onto a string with separators if more than one exists, return an empty string if the items are null or empty.
        Parameters:
        items - Object[]
        separator - String
        Returns:
        String
      • getEmptyStrings

        public static java.lang.String[] getEmptyStrings()
        Returns an empty array of string
        Returns:
        String
      • toCamelCase

        public static java.lang.String toCamelCase​(java.lang.String name)
        Converts the given string to Camel case, that is, removing all spaces, and capitalising the first letter of each word except the first.

        If the first word starts with an uppercase letter, it's kept as is. This method can thus be used for Pascal case too.

        Parameters:
        name - The string to convert
        Returns:
        The string converted to Camel case
      • toCamelCase

        public static java.lang.String toCamelCase​(java.lang.String name,
                                                   boolean forceLowerCase)
        Converts the given string to Camel case, that is, removing all spaces, and capitalising the first letter of each word except the first.

        The second parameter can be used to force the words to be converted to lowercase before capitalising. This can be useful if eg the first word contains several uppercase letters.

        Parameters:
        name - The string to convert
        forceLowerCase - Whether to force removal of all upper case letters except on word start
        Returns:
        The string converted to Camel case