Class StringTools


  • public class StringTools
    extends Object
    Utility class for formatting strings into regular-sized blocks.
    Since:
    1.5
    Author:
    Richard Holland
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String leftIndent​(String input, int leftIndent)
      Takes an input string and appends spaces to the left.
      static String leftPad​(String input, char padChar, int totalWidth)
      Pads a string to be a certain width by prepending given symbols.
      static String leftPad​(String input, int totalWidth)
      Pads a string to be a certain width by prepending spaces.
      static String rightPad​(String input, char padChar, int totalWidth)
      Pads a string to be a certain width by appending given symbols.
      static String rightPad​(String input, int totalWidth)
      Pads a string to be a certain width by appending spaces.
      static String[] wordWrap​(String input, String sepRegex, int width)
      Word-wraps a string into an array of lines of no more than the given width.
      static void writeKeyValueLine​(String key, String text, int keyWidth, int lineWidth, PrintStream os)
      Writes some text to the output stream in the following format: key text continuation of text where the key/wrappedKey column is keyWidth wide, and the total line width is lineWidth, and the text is split over multiple lines at the nearest occurrence of whitespace.
      static void writeKeyValueLine​(String key, String text, int keyWidth, int lineWidth, String sep, PrintStream os)
      Writes some text to the output stream in the following format: key text continuation of text where the key/wrappedKey column is keyWidth wide, and the total line width is lineWidth, and the text is split over multiple lines at the nearest occurrence of separator sep.
      static void writeKeyValueLine​(String key, String text, int keyWidth, int lineWidth, String sep, String wrappedKey, PrintStream os)
      Writes some text to the output stream in the following format: key text wrappedKey continuation of text where the key/wrappedKey column is keyWidth wide, and the total line width is lineWidth, and the text is split over multiple lines at the nearest occurrence of separator sep.
    • Method Detail

      • leftIndent

        public static String leftIndent​(String input,
                                        int leftIndent)
        Takes an input string and appends spaces to the left. Ignores any existing leading whitespace when counting the indent size.
        Parameters:
        input - the input string
        leftIndent - the number of spaces to indent it by.
        Returns:
        the indented string.
      • leftPad

        public static String leftPad​(String input,
                                     int totalWidth)
        Pads a string to be a certain width by prepending spaces.
        Parameters:
        input - the string to pad.
        totalWidth - the final width required including padded space.
      • leftPad

        public static String leftPad​(String input,
                                     char padChar,
                                     int totalWidth)
        Pads a string to be a certain width by prepending given symbols.
        Parameters:
        input - the string to pad.
        padChar - the symbol to pad with.
        totalWidth - the final width required including padded symbols.
      • rightPad

        public static String rightPad​(String input,
                                      int totalWidth)
        Pads a string to be a certain width by appending spaces.
        Parameters:
        input - the string to pad.
        totalWidth - the final width required including padded space.
      • rightPad

        public static String rightPad​(String input,
                                      char padChar,
                                      int totalWidth)
        Pads a string to be a certain width by appending given symbols.
        Parameters:
        input - the string to pad.
        padChar - the symbol to pad with.
        totalWidth - the final width required including padded symbols.
      • wordWrap

        public static String[] wordWrap​(String input,
                                        String sepRegex,
                                        int width)
        Word-wraps a string into an array of lines of no more than the given width. The string is split into chunks using the regex supplied to identify the points where it can be broken. If a word is longer than the width required, it is broken mid-word, otherwise the string is always broken between words.
        Parameters:
        input - the string to format
        sepRegex - the regex identifying the break points in the string, to be compiled using Pattern.
        width - the width of the lines required
        Returns:
        an array of strings, one per line, containing the wrapped output.
        See Also:
        Pattern
      • writeKeyValueLine

        public static void writeKeyValueLine​(String key,
                                             String text,
                                             int keyWidth,
                                             int lineWidth,
                                             PrintStream os)
        Writes some text to the output stream in the following format: key text continuation of text where the key/wrappedKey column is keyWidth wide, and the total line width is lineWidth, and the text is split over multiple lines at the nearest occurrence of whitespace.
        Parameters:
        key - the key to write on the first line only
        text - the text to write out
        keyWidth - the width to indent the text by (in which the key will be printed)
        os - the stream to write the formatted output to
      • writeKeyValueLine

        public static void writeKeyValueLine​(String key,
                                             String text,
                                             int keyWidth,
                                             int lineWidth,
                                             String sep,
                                             PrintStream os)
        Writes some text to the output stream in the following format: key text continuation of text where the key/wrappedKey column is keyWidth wide, and the total line width is lineWidth, and the text is split over multiple lines at the nearest occurrence of separator sep.
        Parameters:
        key - the key to write on the first line only
        text - the text to write out
        keyWidth - the width to indent the text by (in which the key will be printed)
        sep - the separator to split the text on if it exceeds the line width
        os - the stream to write the formatted output to
      • writeKeyValueLine

        public static void writeKeyValueLine​(String key,
                                             String text,
                                             int keyWidth,
                                             int lineWidth,
                                             String sep,
                                             String wrappedKey,
                                             PrintStream os)
        Writes some text to the output stream in the following format: key text wrappedKey continuation of text where the key/wrappedKey column is keyWidth wide, and the total line width is lineWidth, and the text is split over multiple lines at the nearest occurrence of separator sep.
        Parameters:
        key - the key to write on the first line only
        text - the text to write out
        keyWidth - the width to indent the text by (in which the key will be printed)
        sep - the separator to split the text on if it exceeds the line width
        wrappedKey - the key to print on second and subsequent lines
        os - the stream to write the formatted output to