Package blbutil

Class StringUtil


  • public class StringUtil
    extends java.lang.Object
    Class StringUtil is a utility class with static methods for counting and returning delimited fields in a string.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int countFields​(java.lang.String s)
      Returns the number of white-space delimited fields in the specified string.
      static int countFields​(java.lang.String s, char delimiter)
      Returns the number of delimited fields in the specified string.
      static int countFields​(java.lang.String s, char delimiter, int max)
      Returns Math.min(countFields(s, delimiter), max).
      static java.lang.String[] getFields​(java.lang.String s)
      Returns an array obtained by trimming white-space from the beginning and end of the specified string, and splitting the resulting string around white space.
      static java.lang.String[] getFields​(java.lang.String s, char delimiter)
      Returns an array obtained by splitting the specified string around the specified delimiter.
      static java.lang.String[] getFields​(java.lang.String s, char delimiter, int limit)
      Returns an array obtained by splitting the specified string around the first (limit - 1) occurrences of the specified delimiter.
      static java.lang.String[] getFields​(java.lang.String s, int limit)
      Returns an array obtained by trimming white-space from the beginning and end of the specified string, and splitting the resulting string around the first (limit-1) white-space delimiters.
      • Methods inherited from class java.lang.Object

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

      • countFields

        public static int countFields​(java.lang.String s,
                                      char delimiter)
        Returns the number of delimited fields in the specified string. Returns 0 if the specified string has length 0.
        Parameters:
        s - a string
        delimiter - a delimiter character
        Returns:
        the number of delimited fields in the specified string
        Throws:
        java.lang.NullPointerException - if s == null
      • countFields

        public static int countFields​(java.lang.String s,
                                      char delimiter,
                                      int max)
        Returns Math.min(countFields(s, delimiter), max).
        Parameters:
        s - a string with 0 or more delimiter characters
        delimiter - the delimiter character
        max - the maximum value that can be returned
        Returns:
        Math.min(countFields(s, delimiter), max)
        Throws:
        java.lang.NullPointerException - if s == null
      • getFields

        public static java.lang.String[] getFields​(java.lang.String s,
                                                   char delimiter)
        Returns an array obtained by splitting the specified string around the specified delimiter. The array returned by this method contains each substring of the string that does not contain the delimiter and that is preceded by the delimiter or the beginning of the string and that is terminated by the delimiter or the end of the string. The substrings in the array are in the order in which they occur in the specified string. If there are no delimiters in the specified string then the method return an array of length one, whose single element is the specified string.
        Parameters:
        s - a string
        delimiter - a delimiter character
        Returns:
        the array of strings obtained by splitting the specified string around the specified delimiter
        Throws:
        java.lang.NullPointerException - if s == null
      • getFields

        public static java.lang.String[] getFields​(java.lang.String s,
                                                   char delimiter,
                                                   int limit)
        Returns an array obtained by splitting the specified string around the first (limit - 1) occurrences of the specified delimiter. If the string contains fewer than (limit - 1) delimiter characters, the returned value will equal StringUtil.getFields(s, delimiter)
        Parameters:
        s - a string
        delimiter - a delimiter character
        limit - the maximum length of the returned array
        Returns:
        an array obtained by splitting the specified string around the specified delimiter
        Throws:
        java.lang.NullPointerException - if s == null
        java.lang.IllegalArgumentException - if limit < 2
      • countFields

        public static int countFields​(java.lang.String s)
        Returns the number of white-space delimited fields in the specified string. A field is a maximal set of consecutive characters that are not white space characters. White space is defined as any unicode characters less than or equal to '\u0020'.
        Parameters:
        s - a string
        Returns:
        the number of white-space delimited fields in the specified string
        Throws:
        java.lang.NullPointerException - if s == null
      • getFields

        public static java.lang.String[] getFields​(java.lang.String s)
        Returns an array obtained by trimming white-space from the beginning and end of the specified string, and splitting the resulting string around white space. White space is any maximal substring of unicode characters less than or equal to '\u0020'. White-space at the beginning and end of the string is ignored. The substrings in the returned array are in the order in which they occur in this string. If there is no white-space in the specified string, the method returns an array of length one whose single element is the trimmed string. If the specified string contains only white-space a string array of length 0 is returned.
        Parameters:
        s - a string
        Returns:
        the array of strings obtained by splitting the specified string around white space
        Throws:
        java.lang.NullPointerException - if s == null
      • getFields

        public static java.lang.String[] getFields​(java.lang.String s,
                                                   int limit)

        Returns an array obtained by trimming white-space from the beginning and end of the specified string, and splitting the resulting string around the first (limit-1) white-space delimiters. A white-space delimiter is any maximal substring of unicode characters less than or equal to '\u0020'. If the trimmed string contains fewer than (limit - 1) white space delimiters, the returned value will equal StringUtil.getFields(s). The substrings in the returned array are in the order in which they occur in this string. If there are no white-space delimiters in the specified string, the method returns an array of length one whose single element is the trimmed string. If the specified string contains only white-space, a string array of length 0 is returned.

        Parameters:
        s - a string
        limit - the maximum length of the returned array
        Returns:
        the array of strings obtained by splitting the specified string around white space
        Throws:
        java.lang.NullPointerException - if s == null
        java.lang.IllegalArgumentException - if limit < 2