Class CharUtil


  • public class CharUtil
    extends java.lang.Object
    Common character and console utilities. It can contain both standard unicode characters and unix console control sequences. Here also resides lots of helper methods related to calculating visible string length, that takes into account control sequences, non-width characters and double width characters.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int TAB_WIDTH  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Char alt​(char c)  
      static java.lang.String center​(java.lang.String string, int width)
      Pad the each side of the string until the printed width becomes the desired visible string length.
      static java.lang.String clipWidth​(java.lang.String string, int width)
      Remove all printable characters after 'width' characters have been filled.
      static java.lang.String expandTabs​(java.lang.String string)
      Expand tabs in string.
      static java.lang.String expandTabs​(java.lang.String string, int tabWidth)
      Expand tabs in string.
      static java.lang.String expandTabs​(java.lang.String string, int tabWidth, int offset)
      Expand tabs in string.
      static byte[] inputBytes​(java.lang.Object... in)
      Make a byte array representing the input bytes for generating the given input.
      static java.util.List<Char> inputChars​(java.lang.Object... in)
      Make a list of input Chars that e.g. can be used in testing input or output.
      static java.lang.String leftPad​(java.lang.String string, int width)
      Pad the right side of the string until the printed width becomes the desired visible string length.
      static Unicode makeBorder​(int u, int r, int d, int l)
      Make a uncode boder symbol matching the lines given.
      static Unicode makeNumeric​(int num)
      Gets the unicode char representing a circled number.
      static int printableWidth​(java.lang.String string)
      How many single-characters worth of console real-estate will be taken up by this string if printed.
      static java.lang.String rightPad​(java.lang.String string, int width)
      Pad the right side of the string until the printed width becomes the desired visible string length.
      static java.lang.String stripNonPrintable​(java.lang.String string)
      Strip string of all non-printable characters.
      • Methods inherited from class java.lang.Object

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

      • printableWidth

        public static int printableWidth​(java.lang.String 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 java.lang.String expandTabs​(java.lang.String string)
        Expand tabs in string.
        Parameters:
        string - The string to expand.
        Returns:
        The expanded string.
      • expandTabs

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

        public static java.lang.String expandTabs​(java.lang.String 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 java.lang.String stripNonPrintable​(java.lang.String string)
        Strip string of all non-printable characters.
        Parameters:
        string - The source string.
        Returns:
        The result without non-printable chars.
      • clipWidth

        public static java.lang.String clipWidth​(java.lang.String 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 java.lang.String rightPad​(java.lang.String 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 java.lang.String leftPad​(java.lang.String 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.
      • center

        public static java.lang.String center​(java.lang.String string,
                                              int width)
        Pad the 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.
      • makeBorder

        public static Unicode makeBorder​(int u,
                                         int r,
                                         int d,
                                         int l)
        Make a uncode boder symbol matching the lines given. The four valies are for top (upward line), right, bottom and left. 0 - no line. 1 - thin line 2 - thick line 3 - double line
        Parameters:
        u - The upward line.
        r - The rightward line.
        d - The downward line.
        l - The leftward line.
        Returns:
        The unicode instance, or null if none matching.
      • makeNumeric

        public static Unicode makeNumeric​(int num)
        Gets the unicode char representing a circled number.
        Parameters:
        num - Number to get unicode char for.
        Returns:
        The unicode char representation.
      • inputBytes

        public static byte[] inputBytes​(java.lang.Object... in)
        Make a byte array representing the input bytes for generating the given input. See CharReader, CharStream. The input accepts: Character (char), Integer and any Char instance as input. And everything else is Object.toString()'ed and handled as a UTF-8 string.
        Parameters:
        in - The input objects.
        Returns:
        The input bytes.
      • inputChars

        public static java.util.List<Char> inputChars​(java.lang.Object... in)
        Make a list of input Chars that e.g. can be used in testing input or output.
        Parameters:
        in - the input objects.
        Returns:
        The char list representing the input.
      • alt

        public static Char alt​(char c)