Class StringUtil


  • public final class StringUtil
    extends Object
    Utility getting properties of and modifying strings.
    • Field Detail

      • TAB_WIDTH

        public static final int TAB_WIDTH
        Default tab width in number of spaces.
        See Also:
        Constant Field Values
    • Method Detail

      • isNullOrEmpty

        public static boolean isNullOrEmpty​(CharSequence str)
        Check if string is null or empty.
        Parameters:
        str - The string to check.
        Returns:
        If the string is null or empty.
      • isNotEmpty

        public static boolean isNotEmpty​(CharSequence str)
        Check if string is not empty.
        Parameters:
        str - The string to check.
        Returns:
        If the string is non-null and not empty.
      • emptyToNull

        public static String emptyToNull​(CharSequence str)
        Make empty strings into null values.
        Parameters:
        str - The string to check.
        Returns:
        The string if it has content, null otherwise.
      • capitalize

        public static String capitalize​(CharSequence s)
        Capitalize the string. Assuming the first 16-bit char can be upper-cased alone.
        Parameters:
        s - The source string.
        Returns:
        The capitalized string.
      • toUpperCase

        public static String toUpperCase​(CharSequence s)
        Make the string into upper-case using US locale.
        Parameters:
        s - The source string.
        Returns:
        The upper-cased string.
      • toLowerCase

        public static String toLowerCase​(CharSequence s)
        Make the string into lower-case using US locale.
        Parameters:
        s - The source string.
        Returns:
        The lower-cased string.
      • toWhitespace

        public static String toWhitespace​(CharSequence s)
        Generates whitespace-only string with the same printable length as the source. Handy when printing aligned strings and need to align spaces with a string value.
        Parameters:
        s - The source string.
        Returns:
        A whitespace string of same length.
      • printableWidth

        public static int printableWidth​(CharSequence string)
        How many single-characters worth of console real-estate will be taken up by this string if printed. Control characters will be ignored, and double-width characters (CJK) will count as 2 width each.

        Strings containing carriage movement, CR, LF, unexpanded tabs etc are not allowed, and will cause an IllegalArgumentException.

        Parameters:
        string - The string to measure.
        Returns:
        The printed width.
      • expandTabs

        public static String expandTabs​(CharSequence string)
        Expand tabs in string.
        Parameters:
        string - The string to expand.
        Returns:
        The expanded string.
      • expandTabs

        public static String expandTabs​(CharSequence string,
                                        int tabWidth)
        Expand tabs in string.
        Parameters:
        string - The string to expand.
        tabWidth - The tab width.
        Returns:
        The expanded string.
      • expandTabs

        public static String expandTabs​(CharSequence string,
                                        int tabWidth,
                                        int offset)
        Expand tabs in string.
        Parameters:
        string - The string to expand.
        tabWidth - The tab width.
        offset - The initial offset.
        Returns:
        The expanded string.
      • stripNonPrintable

        public static String stripNonPrintable​(CharSequence string)
        Strip string of all non-printable characters.
        Parameters:
        string - The source string.
        Returns:
        The result without non-printable chars.
      • clipWidth

        public static String clipWidth​(CharSequence string,
                                       int width)
        Remove all printable characters after 'width' characters have been filled. All control chars will be left in place.
        Parameters:
        string - The base string.
        width - The printed width.
        Returns:
        The clipped string.
      • rightPad

        public static String rightPad​(CharSequence string,
                                      int width)
        Pad the right side of the string until the printed width becomes the desired visible string length.
        Parameters:
        string - The string to just.
        width - The total printable width to fill.
        Returns:
        The padded string.
      • leftPad

        public static String leftPad​(CharSequence string,
                                     int width)
        Pad the left side of the string until the printed width becomes the desired visible string length.
        Parameters:
        string - The string to just.
        width - The total printable width to fill.
        Returns:
        The padded string.
      • center

        public static String center​(CharSequence string,
                                    int width)
        Pad each side of the string until the printed width becomes the desired visible string length.
        Parameters:
        string - The string to just.
        width - The total printable width to fill.
        Returns:
        The padded string.
      • wrap

        public static String wrap​(CharSequence text,
                                  int width)
        This will attempt to print out the text line wrapped, using the printable width to figure out where to cut each line. It is assumed the entire text consists of a single paragraph of text.
        Parameters:
        text - The string to be wrapped. This text will be the making of this method normalize all spacing, and allow splitting on '-'
        width - The total width of the text.
        Returns:
        The wrapped text.
      • wrap

        public static String wrap​(CharSequence prefix,
                                  CharSequence indent,
                                  CharSequence text,
                                  int width)
        This will attempt to print out the text line wrapped, using the printable width to figure out where to cut each line. It is assumed the entire text consists of a single paragraph of text.
        Parameters:
        prefix - String to come before the text on the first line only.
        indent - String to prefix the text any subsequent lines.
        text - The string to be wrapped. This text will be the making of this method normalize all spacing, and allow splitting on '-'
        width - The total width of the text, including prefix / indent.
        Returns:
        The wrapped text.
      • longestCommonPrefixPath

        public static String longestCommonPrefixPath​(Collection<String> paths)
        Assuming all strings in the path input are '/' separated paths, will find the longest common path prefix (meaning the prefix ends in '/').
        Parameters:
        paths - Paths to find the longest common prefix for.
        Returns:
        The longest common prefix.
      • stripCommonPrefixPath

        public static List<String> stripCommonPrefixPath​(Collection<String> paths)
        Assuming all strings in the path input are '/' separated paths, will find the longest common path prefix (meaning the prefix ends in '/') and remove that prefix from all the input paths.
        Parameters:
        paths - Paths to find the longest common prefix for.
        Returns:
        The longest common prefix.
      • commonPrefix

        public static int commonPrefix​(String text1,
                                       String text2)
        Determine the common prefix of two strings
        Parameters:
        text1 - First string.
        text2 - Second string.
        Returns:
        The number of characters common to the start of each string.
      • commonSuffix

        public static int commonSuffix​(String text1,
                                       String text2)
        Determine the common suffix of two strings
        Parameters:
        text1 - First string.
        text2 - Second string.
        Returns:
        The number of characters common to the end of each string.
      • commonOverlap

        public static int commonOverlap​(String text1,
                                        String text2)
        Determine if the suffix of one string is the prefix of another.
        Parameters:
        text1 - First string.
        text2 - Second string.
        Returns:
        The number of characters common to the end of the first string and the start of the second string.