Class StringUtils


  • class StringUtils
    extends java.lang.Object

    Common String manipulation routines, extracted from Plexus Utils and trimmed down for Plexus Xml.

    Originally from Turbine and the GenerationJavaCore library.

    Since:
    1.0
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private StringUtils()
      StringUtils instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isEmpty​(java.lang.String str)
      Checks if a String is null or empty.
      static java.lang.String removeDuplicateWhitespace​(java.lang.String s)
      Remove all duplicate whitespace characters and line terminators are replaced with a single space.
      static java.lang.String repeat​(java.lang.String str, int repeat)
      Repeat a String n times to form a new string.
      static java.lang.String[] split​(java.lang.String text, java.lang.String separator)  
      private static java.lang.String[] split​(java.lang.String str, java.lang.String separator, int max)
      Splits the provided text into a array, based on a given separator.
      static java.lang.String unifyLineSeparators​(java.lang.String s, java.lang.String ls)
      Parses the given String and replaces all occurrences of '\n', '\r' and '\r\n' with the system line separator.
      • Methods inherited from class java.lang.Object

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

      • StringUtils

        private StringUtils()

        StringUtils instances should NOT be constructed in standard programming. Instead, the class should be used as StringUtils.trim(" foo ");.

        This constructor is public to permit tools that require a JavaBean manager to operate.

    • Method Detail

      • isEmpty

        public static boolean isEmpty​(java.lang.String str)
        Checks if a String is null or empty.

        Note: In releases prior 3.5.0, this method trimmed the input string such that it worked the same as #isBlank(String). Since release 3.5.0 it no longer returns true for strings containing only whitespace characters.

        Parameters:
        str - the String to check
        Returns:
        true if the String is null, or length zero
      • split

        public static java.lang.String[] split​(java.lang.String text,
                                               java.lang.String separator)
        Parameters:
        text - The string to parse.
        separator - Characters used as the delimiters. If null, splits on whitespace.
        Returns:
        an array of parsed Strings
      • split

        private static java.lang.String[] split​(java.lang.String str,
                                                java.lang.String separator,
                                                int max)

        Splits the provided text into a array, based on a given separator.

        The separator is not included in the returned String array. The maximum number of splits to perform can be controlled. A null separator will cause parsing to be on whitespace.

        This is useful for quickly splitting a String directly into an array of tokens, instead of an enumeration of tokens (as StringTokenizer does).

        Parameters:
        str - The string to parse.
        separator - Characters used as the delimiters. If null, splits on whitespace.
        max - The maximum number of elements to include in the array. A zero or negative value implies no limit.
        Returns:
        an array of parsed Strings
      • repeat

        public static java.lang.String repeat​(java.lang.String str,
                                              int repeat)

        Repeat a String n times to form a new string.

        Parameters:
        str - String to repeat
        repeat - number of times to repeat str
        Returns:
        String with repeated String
        Throws:
        java.lang.NegativeArraySizeException - if repeat < 0
        java.lang.NullPointerException - if str is null
      • removeDuplicateWhitespace

        public static java.lang.String removeDuplicateWhitespace​(java.lang.String s)
        Remove all duplicate whitespace characters and line terminators are replaced with a single space.
        Parameters:
        s - a not null String
        Returns:
        a string with unique whitespace.
        Since:
        1.5.7
      • unifyLineSeparators

        public static java.lang.String unifyLineSeparators​(java.lang.String s,
                                                           java.lang.String ls)
        Parses the given String and replaces all occurrences of '\n', '\r' and '\r\n' with the system line separator.
        Parameters:
        s - a not null String
        ls - the wanted line separator ("\n" on UNIX), if null using the System line separator.
        Returns:
        a String that contains only System line separators.
        Throws:
        java.lang.IllegalArgumentException - if ls is not '\n', '\r' and '\r\n' characters.
        Since:
        1.5.7