Package 

Class CharUtils


  • 
    public class CharUtils
    
                        

    Operations on char primitives and Character objects.

    This class tries to handle {@code null} input gracefully. An exception will not be thrown for a {@code null} input. Each method documents its behaviour in more detail.

    #ThreadSafe#

    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static char LF
      public final static char CR
    • Constructor Summary

      Constructors 
      Constructor Description
      CharUtils() {@code CharUtils} instances should NOT be constructed in standard programming.
    • Method Summary

      Modifier and Type Method Description
      static Character toCharacterObject(char ch) Converts the character to a Character.
      static Character toCharacterObject(String str) Converts the String to a Character using the first character, returningnull for empty Strings.
      static char toChar(Character ch) Converts the Character to a char throwing an exception for {@code null}.
      static char toChar(Character ch, char defaultValue) Converts the Character to a char handling {@code null}.
      static char toChar(String str) Converts the String to a char using the first character, throwingan exception on empty Strings.
      static char toChar(String str, char defaultValue) Converts the String to a char using the first character, defaultingthe value on empty Strings.
      static int toIntValue(char ch) Converts the character to the Integer it represents, throwing anexception if the character is not numeric.
      static int toIntValue(char ch, int defaultValue) Converts the character to the Integer it represents, throwing anexception if the character is not numeric.
      static int toIntValue(Character ch) Converts the character to the Integer it represents, throwing anexception if the character is not numeric.
      static int toIntValue(Character ch, int defaultValue) Converts the character to the Integer it represents, throwing anexception if the character is not numeric.
      static String toString(char ch) Converts the character to a String that contains the one character.
      static String toString(Character ch) Converts the character to a String that contains the one character.
      static String unicodeEscaped(char ch) Converts the string to the Unicode format '\u0020'.
      static String unicodeEscaped(Character ch) Converts the string to the Unicode format '\u0020'.
      static boolean isAscii(char ch) Checks whether the character is ASCII 7 bit.
      static boolean isAsciiPrintable(char ch) Checks whether the character is ASCII 7 bit printable.
      static boolean isAsciiControl(char ch) Checks whether the character is ASCII 7 bit control.
      static boolean isAsciiAlpha(char ch) Checks whether the character is ASCII 7 bit alphabetic.
      static boolean isAsciiAlphaUpper(char ch) Checks whether the character is ASCII 7 bit alphabetic upper case.
      static boolean isAsciiAlphaLower(char ch) Checks whether the character is ASCII 7 bit alphabetic lower case.
      static boolean isAsciiNumeric(char ch) Checks whether the character is ASCII 7 bit numeric.
      static boolean isAsciiAlphanumeric(char ch) Checks whether the character is ASCII 7 bit numeric.
      • Methods inherited from class java.lang.Object

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

      • CharUtils

        CharUtils()
        {@code CharUtils} instances should NOT be constructed in standard programming.
    • Method Detail

      • toCharacterObject

        @Deprecated() static Character toCharacterObject(char ch)

        Converts the character to a Character.

        For ASCII 7 bit characters, this uses a cache that will return thesame Character object each time.

          CharUtils.toCharacterObject(' ')  = ' '
          CharUtils.toCharacterObject('A')  = 'A'
        
        Parameters:
        ch - the character to convert
      • toCharacterObject

         static Character toCharacterObject(String str)

        Converts the String to a Character using the first character, returningnull for empty Strings.

        For ASCII 7 bit characters, this uses a cache that will return thesame Character object each time.

          CharUtils.toCharacterObject(null) = null
          CharUtils.toCharacterObject("")   = null
          CharUtils.toCharacterObject("A")  = 'A'
          CharUtils.toCharacterObject("BA") = 'B'
        
        Parameters:
        str - the character to convert
      • toChar

         static char toChar(Character ch)

        Converts the Character to a char throwing an exception for {@code null}.

          CharUtils.toChar(' ')  = ' '
          CharUtils.toChar('A')  = 'A'
          CharUtils.toChar(null) throws IllegalArgumentException
        
        Parameters:
        ch - the character to convert
      • toChar

         static char toChar(Character ch, char defaultValue)

        Converts the Character to a char handling {@code null}.

          CharUtils.toChar(null, 'X') = 'X'
          CharUtils.toChar(' ', 'X')  = ' '
          CharUtils.toChar('A', 'X')  = 'A'
        
        Parameters:
        ch - the character to convert
        defaultValue - the value to use if the Character is null
      • toChar

         static char toChar(String str)

        Converts the String to a char using the first character, throwingan exception on empty Strings.

          CharUtils.toChar("A")  = 'A'
          CharUtils.toChar("BA") = 'B'
          CharUtils.toChar(null) throws IllegalArgumentException
          CharUtils.toChar("")   throws IllegalArgumentException
        
        Parameters:
        str - the character to convert
      • toChar

         static char toChar(String str, char defaultValue)

        Converts the String to a char using the first character, defaultingthe value on empty Strings.

          CharUtils.toChar(null, 'X') = 'X'
          CharUtils.toChar("", 'X')   = 'X'
          CharUtils.toChar("A", 'X')  = 'A'
          CharUtils.toChar("BA", 'X') = 'B'
        
        Parameters:
        str - the character to convert
        defaultValue - the value to use if the Character is null
      • toIntValue

         static int toIntValue(char ch)

        Converts the character to the Integer it represents, throwing anexception if the character is not numeric.

        This method coverts the char '1' to the int 1 and so on.

          CharUtils.toIntValue('3')  = 3
          CharUtils.toIntValue('A')  throws IllegalArgumentException
        
        Parameters:
        ch - the character to convert
      • toIntValue

         static int toIntValue(char ch, int defaultValue)

        Converts the character to the Integer it represents, throwing anexception if the character is not numeric.

        This method coverts the char '1' to the int 1 and so on.

          CharUtils.toIntValue('3', -1)  = 3
          CharUtils.toIntValue('A', -1)  = -1
        
        Parameters:
        ch - the character to convert
        defaultValue - the default value to use if the character is not numeric
      • toIntValue

         static int toIntValue(Character ch)

        Converts the character to the Integer it represents, throwing anexception if the character is not numeric.

        This method coverts the char '1' to the int 1 and so on.

          CharUtils.toIntValue('3')  = 3
          CharUtils.toIntValue(null) throws IllegalArgumentException
          CharUtils.toIntValue('A')  throws IllegalArgumentException
        
        Parameters:
        ch - the character to convert, not null
      • toIntValue

         static int toIntValue(Character ch, int defaultValue)

        Converts the character to the Integer it represents, throwing anexception if the character is not numeric.

        This method coverts the char '1' to the int 1 and so on.

          CharUtils.toIntValue(null, -1) = -1
          CharUtils.toIntValue('3', -1)  = 3
          CharUtils.toIntValue('A', -1)  = -1
        
        Parameters:
        ch - the character to convert
        defaultValue - the default value to use if the character is not numeric
      • toString

         static String toString(char ch)

        Converts the character to a String that contains the one character.

        For ASCII 7 bit characters, this uses a cache that will return thesame String object each time.

          CharUtils.toString(' ')  = " "
          CharUtils.toString('A')  = "A"
        
        Parameters:
        ch - the character to convert
      • toString

         static String toString(Character ch)

        Converts the character to a String that contains the one character.

        For ASCII 7 bit characters, this uses a cache that will return thesame String object each time.

        If {@code null} is passed in, {@code null} will be returned.

          CharUtils.toString(null) = null
          CharUtils.toString(' ')  = " "
          CharUtils.toString('A')  = "A"
        
        Parameters:
        ch - the character to convert
      • unicodeEscaped

         static String unicodeEscaped(char ch)

        Converts the string to the Unicode format '\u0020'.

        This format is the Java source code format.

          CharUtils.unicodeEscaped(' ') = "\u0020"
          CharUtils.unicodeEscaped('A') = "\u0041"
        
        Parameters:
        ch - the character to convert
      • unicodeEscaped

         static String unicodeEscaped(Character ch)

        Converts the string to the Unicode format '\u0020'.

        This format is the Java source code format.

        If {@code null} is passed in, {@code null} will be returned.

          CharUtils.unicodeEscaped(null) = null
          CharUtils.unicodeEscaped(' ')  = "\u0020"
          CharUtils.unicodeEscaped('A')  = "\u0041"
        
        Parameters:
        ch - the character to convert, may be null
      • isAscii

         static boolean isAscii(char ch)

        Checks whether the character is ASCII 7 bit.

          CharUtils.isAscii('a')  = true
          CharUtils.isAscii('A')  = true
          CharUtils.isAscii('3')  = true
          CharUtils.isAscii('-')  = true
          CharUtils.isAscii('\n') = true
          CharUtils.isAscii('©') = false
        
        Parameters:
        ch - the character to check
      • isAsciiPrintable

         static boolean isAsciiPrintable(char ch)

        Checks whether the character is ASCII 7 bit printable.

          CharUtils.isAsciiPrintable('a')  = true
          CharUtils.isAsciiPrintable('A')  = true
          CharUtils.isAsciiPrintable('3')  = true
          CharUtils.isAsciiPrintable('-')  = true
          CharUtils.isAsciiPrintable('\n') = false
          CharUtils.isAsciiPrintable('©') = false
        
        Parameters:
        ch - the character to check
      • isAsciiControl

         static boolean isAsciiControl(char ch)

        Checks whether the character is ASCII 7 bit control.

          CharUtils.isAsciiControl('a')  = false
          CharUtils.isAsciiControl('A')  = false
          CharUtils.isAsciiControl('3')  = false
          CharUtils.isAsciiControl('-')  = false
          CharUtils.isAsciiControl('\n') = true
          CharUtils.isAsciiControl('©') = false
        
        Parameters:
        ch - the character to check
      • isAsciiAlpha

         static boolean isAsciiAlpha(char ch)

        Checks whether the character is ASCII 7 bit alphabetic.

          CharUtils.isAsciiAlpha('a')  = true
          CharUtils.isAsciiAlpha('A')  = true
          CharUtils.isAsciiAlpha('3')  = false
          CharUtils.isAsciiAlpha('-')  = false
          CharUtils.isAsciiAlpha('\n') = false
          CharUtils.isAsciiAlpha('©') = false
        
        Parameters:
        ch - the character to check
      • isAsciiAlphaUpper

         static boolean isAsciiAlphaUpper(char ch)

        Checks whether the character is ASCII 7 bit alphabetic upper case.

          CharUtils.isAsciiAlphaUpper('a')  = false
          CharUtils.isAsciiAlphaUpper('A')  = true
          CharUtils.isAsciiAlphaUpper('3')  = false
          CharUtils.isAsciiAlphaUpper('-')  = false
          CharUtils.isAsciiAlphaUpper('\n') = false
          CharUtils.isAsciiAlphaUpper('©') = false
        
        Parameters:
        ch - the character to check
      • isAsciiAlphaLower

         static boolean isAsciiAlphaLower(char ch)

        Checks whether the character is ASCII 7 bit alphabetic lower case.

          CharUtils.isAsciiAlphaLower('a')  = true
          CharUtils.isAsciiAlphaLower('A')  = false
          CharUtils.isAsciiAlphaLower('3')  = false
          CharUtils.isAsciiAlphaLower('-')  = false
          CharUtils.isAsciiAlphaLower('\n') = false
          CharUtils.isAsciiAlphaLower('©') = false
        
        Parameters:
        ch - the character to check
      • isAsciiNumeric

         static boolean isAsciiNumeric(char ch)

        Checks whether the character is ASCII 7 bit numeric.

          CharUtils.isAsciiNumeric('a')  = false
          CharUtils.isAsciiNumeric('A')  = false
          CharUtils.isAsciiNumeric('3')  = true
          CharUtils.isAsciiNumeric('-')  = false
          CharUtils.isAsciiNumeric('\n') = false
          CharUtils.isAsciiNumeric('©') = false
        
        Parameters:
        ch - the character to check
      • isAsciiAlphanumeric

         static boolean isAsciiAlphanumeric(char ch)

        Checks whether the character is ASCII 7 bit numeric.

          CharUtils.isAsciiAlphanumeric('a')  = true
          CharUtils.isAsciiAlphanumeric('A')  = true
          CharUtils.isAsciiAlphanumeric('3')  = true
          CharUtils.isAsciiAlphanumeric('-')  = false
          CharUtils.isAsciiAlphanumeric('\n') = false
          CharUtils.isAsciiAlphanumeric('©') = false
        
        Parameters:
        ch - the character to check