Class StringUtils


  • public class StringUtils
    extends Object
    A small collection of utilities for manipulating strings.
    Author:
    Phil DeJarnett
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ALIGN_CENTER
      Represents centered alignment.
      static int ALIGN_LEFT
      Represents left alignment.
      static int ALIGN_RIGHT
      Represents right alignment.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void align​(PrintWriter output, String s, int width, char paddingChar, int alignment)
      Pads out a left-, right-, or center-aligned string using the specified character up to the specified width.
      static void align​(PrintWriter output, String s, int width, int alignment)
      Pads out a left-, right-, or center-aligned string using spaces up to the specified width.
      static String align​(String s, int width, char paddingChar, int alignment)
      Pads out a left-, right-, or center-aligned string using the specified character up to the specified width.
      static String align​(String s, int width, int alignment)
      Pads out a left-, right-, or center-aligned string using spaces up to the specified width.
      static String multiply​(char c, int count)
      Duplicates the given character count times.
      static void multiply​(PrintWriter output, char c, int count)
      Duplicates the given character count times to the Writer.
      static void multiply​(PrintWriter output, String s, int count)
      Duplicates the given string count times to the Writer.
      static String multiply​(String s, int count)
      Duplicates the given string count times.
      static void prependEachLine​(PrintWriter output, String s, String prependWith)
      Prepends a specific string to the front of each line of an input string.
      static String prependEachLine​(String s, String prependWith)
      Prepends a specific string to the front of each line of an input string.
    • Field Detail

      • ALIGN_LEFT

        public static final int ALIGN_LEFT
        Represents left alignment.
        See Also:
        Constant Field Values
      • ALIGN_CENTER

        public static final int ALIGN_CENTER
        Represents centered alignment.
        See Also:
        Constant Field Values
      • ALIGN_RIGHT

        public static final int ALIGN_RIGHT
        Represents right alignment.
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringUtils

        public StringUtils()
    • Method Detail

      • align

        public static String align​(String s,
                                   int width,
                                   int alignment)
        Pads out a left-, right-, or center-aligned string using spaces up to the specified width.
        Parameters:
        s - String to pad
        width - Minimum width of final string
        alignment - How to align the string < 0 means left, 0 means center, and > 0 means right
        Returns:
        Padded string
      • align

        public static String align​(String s,
                                   int width,
                                   char paddingChar,
                                   int alignment)
        Pads out a left-, right-, or center-aligned string using the specified character up to the specified width.
        Parameters:
        s - String to pad
        width - Minimum width of final string
        paddingChar - Character to pad with
        alignment - How to align the string < 0 means left, 0 means center, and > 0 means right
        Returns:
        Padded string
      • align

        public static void align​(PrintWriter output,
                                 String s,
                                 int width,
                                 int alignment)
        Pads out a left-, right-, or center-aligned string using spaces up to the specified width.
        Parameters:
        output - Writer to output the centered string to
        s - String to pad
        width - Minimum width of final string
        alignment - How to align the string < 0 means left, 0 means center, and > 0 means right
      • align

        public static void align​(PrintWriter output,
                                 String s,
                                 int width,
                                 char paddingChar,
                                 int alignment)
        Pads out a left-, right-, or center-aligned string using the specified character up to the specified width.
        Parameters:
        output - Writer to output the centered string to
        s - String to pad
        width - Minimum width of final string
        paddingChar - Character to pad with
        alignment - How to align the string < 0 means left, 0 means center, and > 0 means right
      • multiply

        public static String multiply​(char c,
                                      int count)
        Duplicates the given character count times. If count is less than or equal to 0, the empty string is returned.
        Parameters:
        c - Character to duplicate
        count - Number of times to duplicate
        Returns:
        Duplicated string.
      • multiply

        public static String multiply​(String s,
                                      int count)
        Duplicates the given string count times. If count is less than or equal to 0, the empty string is returned.
        Parameters:
        s - String to duplicate
        count - Number of times to duplicate
        Returns:
        Duplicated string.
      • multiply

        public static void multiply​(PrintWriter output,
                                    char c,
                                    int count)
        Duplicates the given character count times to the Writer. If count is less than or equal to 0, this is a no-op.
        Parameters:
        output - Writer to receive duplicated results
        c - Character to duplicate
        count - Number of times to duplicate
      • multiply

        public static void multiply​(PrintWriter output,
                                    String s,
                                    int count)
        Duplicates the given string count times to the Writer. If count is less than or equal to 0, this is a no-op.
        Parameters:
        output - Writer to receive duplicated results
        s - String to duplicate
        count - Number of times to duplicate
      • prependEachLine

        public static String prependEachLine​(String s,
                                             String prependWith)
        Prepends a specific string to the front of each line of an input string. Lines are marked by a trailing '\n', '\r', or '\f' character.
        Parameters:
        s - The input string.
        prependWith - The string to prepend to each line.
        Returns:
        The modified string.
      • prependEachLine

        public static void prependEachLine​(PrintWriter output,
                                           String s,
                                           String prependWith)
        Prepends a specific string to the front of each line of an input string. Lines are marked by a trailing '\n', '\r', or '\f' character.
        Parameters:
        output - The writer to recieve the modified string.
        s - The input string.
        prependWith - The string to prepend to each line.