Operations on char primitives and Character objects.
This class tries to handle
null
input gracefully.
An exception will not be thrown for a
null
input.
Each method documents its behaviour in more detail.
isAscii
public 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
ch
- the character to check
isAsciiAlpha
public 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
ch
- the character to check
- true if between 65 and 90 or 97 and 122 inclusive
isAsciiAlphaLower
public 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
ch
- the character to check
- true if between 97 and 122 inclusive
isAsciiAlphaUpper
public 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
ch
- the character to check
- true if between 65 and 90 inclusive
isAsciiAlphanumeric
public 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
ch
- the character to check
- true if between 48 and 57 or 65 and 90 or 97 and 122 inclusive
isAsciiControl
public 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
ch
- the character to check
- true if less than 32 or equals 127
isAsciiNumeric
public 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
ch
- the character to check
- true if between 48 and 57 inclusive
isAsciiPrintable
public 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
ch
- the character to check
- true if between 32 and 126 inclusive
toChar
public static char toChar(Character ch)
Converts the Character to a char throwing an exception for
null
.
CharUtils.toChar(null) = IllegalArgumentException
CharUtils.toChar(' ') = ' '
CharUtils.toChar('A') = 'A'
ch
- the character to convert
- the char value of the Character
toChar
public static char toChar(Character ch,
char defaultValue)
Converts the Character to a char handling
null
.
CharUtils.toChar(null, 'X') = 'X'
CharUtils.toChar(' ', 'X') = ' '
CharUtils.toChar('A', 'X') = 'A'
ch
- the character to convertdefaultValue
- the value to use if the Character is null
- the char value of the Character or the default if null
toChar
public static char toChar(String str)
Converts the String to a char using the first character, throwing
an exception on empty Strings.
CharUtils.toChar(null) = IllegalArgumentException
CharUtils.toChar("") = IllegalArgumentException
CharUtils.toChar("A") = 'A'
CharUtils.toChar("BA") = 'B'
str
- the character to convert
- the char value of the first letter of the String
toChar
public static char toChar(String str,
char defaultValue)
Converts the String to a char using the first character, defaulting
the value on empty Strings.
CharUtils.toChar(null, 'X') = 'X'
CharUtils.toChar("", 'X') = 'X'
CharUtils.toChar("A", 'X') = 'A'
CharUtils.toChar("BA", 'X') = 'B'
str
- the character to convertdefaultValue
- the value to use if the Character is null
- the char value of the first letter of the String or the default if null
toCharacterObject
public static Character toCharacterObject(String str)
Converts the String to a Character using the first character, returning
null for empty Strings.
For ASCII 7 bit characters, this uses a cache that will return the
same Character object each time.
CharUtils.toCharacterObject(null) = null
CharUtils.toCharacterObject("") = null
CharUtils.toCharacterObject("A") = 'A'
CharUtils.toCharacterObject("BA") = 'B'
str
- the character to convert
- the Character value of the first letter of the String
toCharacterObject
public static Character toCharacterObject(char ch)
Converts the character to a Character.
For ASCII 7 bit characters, this uses a cache that will return the
same Character object each time.
CharUtils.toCharacterObject(' ') = ' '
CharUtils.toCharacterObject('A') = 'A'
ch
- the character to convert
- a Character of the specified character
toIntValue
public static int toIntValue(Character ch)
Converts the character to the Integer it represents, throwing an
exception if the character is not numeric.
This method coverts the char '1' to the int 1 and so on.
CharUtils.toIntValue(null) = IllegalArgumentException
CharUtils.toIntValue('3') = 3
CharUtils.toIntValue('A') = IllegalArgumentException
ch
- the character to convert, not null
- the int value of the character
toIntValue
public static int toIntValue(Character ch,
int defaultValue)
Converts the character to the Integer it represents, throwing an
exception 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
ch
- the character to convertdefaultValue
- the default value to use if the character is not numeric
- the int value of the character
toIntValue
public static int toIntValue(char ch)
Converts the character to the Integer it represents, throwing an
exception 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') = IllegalArgumentException
ch
- the character to convert
- the int value of the character
toIntValue
public static int toIntValue(char ch,
int defaultValue)
Converts the character to the Integer it represents, throwing an
exception 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
ch
- the character to convertdefaultValue
- the default value to use if the character is not numeric
- the int value of the character
toString
public 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 the
same String object each time.
If
null
is passed in,
null
will be returned.
CharUtils.toString(null) = null
CharUtils.toString(' ') = " "
CharUtils.toString('A') = "A"
ch
- the character to convert
- a String containing the one specified character
toString
public 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 the
same String object each time.
CharUtils.toString(' ') = " "
CharUtils.toString('A') = "A"
ch
- the character to convert
- a String containing the one specified character
unicodeEscaped
public static String unicodeEscaped(Character ch)
Converts the string to the unicode format '\u0020'.
This format is the Java source code format.
If
null
is passed in,
null
will be returned.
CharUtils.unicodeEscaped(null) = null
CharUtils.unicodeEscaped(' ') = "\u0020"
CharUtils.unicodeEscaped('A') = "\u0041"
ch
- the character to convert, may be null
- the escaped unicode string, null if null input
unicodeEscaped
public 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"
ch
- the character to convert
- the escaped unicode string