net.sourceforge.pmd.util

## Class StringUtil

- java.lang.Object
- - net.sourceforge.pmd.util.StringUtil

-     public final class StringUtil
      extends Object

  A number of String-specific utility methods for use by PMD or its IDE plugins.
  Author:
    BrianRemedios

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String[]` | `EMPTY_STRINGS`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `appendXmlEscaped(StringBuilder buf, String src)` Appends to a StringBuilder the String src where non-ASCII and XML special chars are escaped.
    `static void` | `appendXmlEscaped(StringBuilder buf, String src, boolean supportUTF8)`
    `static boolean` | `areSemanticEquals(String a, String b)` Returns true if both strings are effectively null or whitespace, returns false otherwise if they have actual text that differs.
    `static String` | `asString(Object[] items, 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(StringBuffer sb, Iterator<?> iter, String separator)` Copies the elements returned by the iterator onto the string buffer each delimited by the separator.
    `static void` | `asStringOn(StringBuilder sb, Object[] items, String separator)` Copies the array items onto the string builder each delimited by the separator.
    `static String` | `escapeWhitespace(Object o)` Replace some whitespace characters so they are visually apparent.
    `static String` | `htmlEncode(String string)`
    `static boolean` | `isAnyOf(String text, String... tests)` Returns whether the non-null text arg matches any of the test values.
    `static boolean` | `isEmpty(String value)` Returns true if the value arg is either null, empty, or full of whitespace characters.
    `static boolean` | `isNotEmpty(String value)`
    `static boolean` | `isSame(String s1, String s2, boolean trim, boolean ignoreCase, boolean standardizeWhitespace)` Are the two String values the same.
    `static int` | `lengthOfShortestIn(String[] strings)` Return the length of the shortest string in the array.
    `static String` | `lpad(String s, int length)` Left pads a string.
    `static int` | `maxCommonLeadingWhitespaceForAll(String[] strings)` Determine the maximum number of common leading whitespace characters the strings share in the same sequence.
    `static String` | `replaceString(String original, char oldChar, String newString)`
    `static String` | `replaceString(String original, String oldString, String newString)`
    `static boolean` | `startsWithAny(String text, String... prefixes)` Return whether the non-null text arg starts with any of the prefix values.
    `static String[]` | `substringsOf(String source, char delimiter)` Parses the input source using the delimiter specified.
    `static String[]` | `substringsOf(String str, String separator)` Much more efficient than StringTokenizer.
    `static String[]` | `trimStartOn(String[] strings, int trimDepth)` Trims off the leading characters off the strings up to the trimDepth specified.
    `static String` | `withoutPrefixes(String text, 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`

- - ### Field Detail

    - #### EMPTY_STRINGS

          public static final String[] EMPTY_STRINGS

  - ### Method Detail

    - #### startsWithAny

          public static boolean startsWithAny(String text,
                              String... prefixes)

      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)

      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)

      Checks for the existence of any of the listed prefixes on the non-null text and removes them.
      Parameters:
        `text` -
        `prefixes` -
      Returns:
        String

    - #### isEmpty

          public static boolean isEmpty(String value)

      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` -
      Returns:
        `true` if the value is empty, `false` otherwise.

    - #### isNotEmpty

          public static boolean isNotEmpty(String value)

      Parameters:
        `value` - String
      Returns:
        boolean

    - #### areSemanticEquals

          public static boolean areSemanticEquals(String a,
                                  String b)

      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

          public static String replaceString(String original,
                             char oldChar,
                             String newString)

      Parameters:
        `original` - String
        `oldChar` - char
        `newString` - String
      Returns:
        String

    - #### replaceString

          public static String replaceString(String original,
                             String oldString,
                             String newString)

      Parameters:
        `original` - String
        `oldString` - String
        `newString` - String
      Returns:
        String

    - #### appendXmlEscaped

          public static void appendXmlEscaped(StringBuilder buf,
                              String src)

      Appends to a StringBuilder the String src where non-ASCII and XML special chars are escaped.
      Parameters:
        `buf` - The destination XML stream
        `src` - The String to append to the stream

    - #### escapeWhitespace

          public static String escapeWhitespace(Object o)

      Replace some whitespace characters so they are visually apparent.
      Parameters:
        `o` -
      Returns:
        String

    - #### htmlEncode

          public static String htmlEncode(String string)

      Parameters:
        `string` - String
      Returns:
        String

    - #### appendXmlEscaped

          public static void appendXmlEscaped(StringBuilder buf,
                              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`).
      See Also:
        `TODO - unify the method above with the one below public to support unit testing - make this package private, once the unit test classes are in the same package.`

    - #### substringsOf

          public static String[] substringsOf(String source,
                              char delimiter)

      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. FIXME - we're on JDK 1.4 now, can we replace this with String.split?
      Parameters:
        `source` - String
        `delimiter` - char
      Returns:
        String[]

    - #### substringsOf

          public static String[] substringsOf(String str,
                              String separator)

      Much more efficient than StringTokenizer.
      Parameters:
        `str` - String
        `separator` - char
      Returns:
        String[]

    - #### asStringOn

          public static void asStringOn(StringBuffer sb,
                        Iterator<?> iter,
                        String separator)

      Copies the elements returned by the iterator onto the string buffer each delimited by the separator.
      Parameters:
        `sb` - StringBuffer
        `iter` - Iterator
        `separator` - String

    - #### asStringOn

          public static void asStringOn(StringBuilder sb,
                        Object[] items,
                        String separator)

      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

    - #### lengthOfShortestIn

          public static int lengthOfShortestIn(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

    - #### maxCommonLeadingWhitespaceForAll

          public static int maxCommonLeadingWhitespaceForAll(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

    - #### trimStartOn

          public static String[] trimStartOn(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

          public static String lpad(String s,
                    int length)

      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)

      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)

      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

Copyright © 2002-2013 InfoEther. All Rights Reserved.