Class StringUtil


  • @Deprecated
    @InternalApi
    public final class StringUtil
    extends Object
    Deprecated.
    Is internal API
    A number of String-specific utility methods for use by PMD or its IDE plugins.
    Author:
    BrianRemedios
    • Method Detail

      • lineNumberAt

        public static int lineNumberAt​(CharSequence charSeq,
                                       int offsetInclusive)
        Deprecated.
        Returns the (1-based) line number of the character at the given index. Line terminators (\r, \n) are assumed to be on the line they *end* and not on the following line. The method also accepts that the given offset be the length of the string (in which case there's no targeted character), to get the line number of a character that would be inserted at the end of the string.
        
             lineNumberAt("a\nb", 0)  = 1
             lineNumberAt("a\nb", 1)  = 1
             lineNumberAt("a\nb", 2)  = 2
             lineNumberAt("a\nb", 3)  = 2  // charAt(3) doesn't exist though
             lineNumberAt("a\nb", 4)  = -1
        
             lineNumberAt("", 0) = 1
             lineNumberAt("", _) = -1
        
         
        Parameters:
        charSeq - Char sequence
        offsetInclusive - Offset in the sequence of the targeted character. May be the length of the sequence.
        Returns:
        -1 if the offset is not in [0, length], otherwise the line number
      • columnNumberAt

        public static int columnNumberAt​(CharSequence charSeq,
                                         int offsetInclusive)
        Deprecated.
        Returns the (1-based) column number of the character at the given index. Line terminators are by convention taken to be part of the line they end, and not the new line they start. Each character has width 1 (including \t). The method also accepts that the given offset be the length of the string (in which case there's no targeted character), to get the column number of a character that would be inserted at the end of the string.
        
             columnNumberAt("a\nb", 0)  = 1
             columnNumberAt("a\nb", 1)  = 2
             columnNumberAt("a\nb", 2)  = 1
             columnNumberAt("a\nb", 3)  = 2   // charAt(3) doesn't exist though
             columnNumberAt("a\nb", 4)  = -1
        
             columnNumberAt("a\r\n", 2)  = 3
        
         
        Parameters:
        charSeq - Char sequence
        offsetInclusive - Offset in the sequence
        Returns:
        -1 if the offset is not in [0, length], otherwise the column number
      • percentageString

        public static String percentageString​(double val,
                                              int numDecimals)
        Deprecated.
        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:
        IllegalArgumentException - if the double to format is not between 0 and 1
      • startsWithAny

        @Deprecated
        public static boolean startsWithAny​(String text,
                                            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​(String text,
                                      String... tests)
        Deprecated.
        Returns whether the non-null text arg matches any of the test values.
        Parameters:
        text -
        tests -
        Returns:
        boolean
      • withoutPrefixes

        public static String withoutPrefixes​(String text,
                                             String... prefixes)
        Deprecated.
        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​(String value)
        Deprecated.
        StringUtils.isNotBlank(CharSequence)
        Parameters:
        value - String
        Returns:
        boolean
      • isEmpty

        @Deprecated
        public static boolean isEmpty​(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​(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​(String a,
                                                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 String replaceString​(String original,
                                           String oldString,
                                           String newString)
        Deprecated.
        StringUtils.replace(String, String, String)
        Parameters:
        original - String
        oldString - String
        newString - String
        Returns:
        String
      • appendXmlEscaped

        @Deprecated
        public static void appendXmlEscaped​(StringBuilder buf,
                                            String src,
                                            boolean supportUTF8)
        Deprecated.
        for removal. Use Java's XML implementations, that do the escaping, use removedInvalidXml10Characters(String) for fixing invalid characters in XML 1.0 documents or use StringEscapeUtils#escapeXml10(String) from apache commons-text instead.
        Parameters:
        buf -
        src -
        supportUTF8 - override the default setting, whether special characters should be replaced with entities ( false) or should be included as is ( true).
      • removedInvalidXml10Characters

        public static String removedInvalidXml10Characters​(String text)
        Deprecated.
        Remove characters, that are not allowed in XML 1.0 documents.

        Allowed characters are:

        Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
        (see Extensible Markup Language (XML) 1.0 (Fifth Edition)).
      • escapeWhitespace

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

        @Deprecated
        public static String replaceString​(String original,
                                           char oldChar,
                                           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 String[] substringsOf​(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 String[] substringsOf​(String str,
                                            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​(StringBuffer sb,
                                      Iterator<?> iter,
                                      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​(StringBuilder sb,
                                      Object[] items,
                                      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​(String[] strings)
        Deprecated.
        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​(String[] strings)
        Deprecated.
        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 String[] trimStartOn​(String[] strings,
                                           int trimDepth)
        Deprecated.
        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 String lpad​(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​(String s1,
                                     String s2,
                                     boolean trim,
                                     boolean ignoreCase,
                                     boolean standardizeWhitespace)
        Deprecated.
        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 String asString​(Object[] items,
                                      String separator)
        Deprecated.
        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
      • removeSurrounding

        public static String removeSurrounding​(String string,
                                               char delimiter)
        Deprecated.
        If the string starts and ends with the delimiter, returns the substring within the delimiters. Otherwise returns the original string. The start and end delimiter must be 2 separate instances.
        
         removeSurrounding("",     _ )  = ""
         removeSurrounding("q",   'q')  = "q"
         removeSurrounding("qq",  'q')  = ""
         removeSurrounding("q_q", 'q')  = "_"
         
      • removeDoubleQuotes

        public static String removeDoubleQuotes​(String string)
        Deprecated.
        Like removeSurrounding with a double quote as a delimiter.
      • getEmptyStrings

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

        public static String toCamelCase​(String name)
        Deprecated.
        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 String toCamelCase​(String name,
                                         boolean forceLowerCase)
        Deprecated.
        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
      • escapeJava

        public static String escapeJava​(String str)
        Deprecated.
        Replaces unprintable characters by their escaped (or unicode escaped) equivalents in the given string