public class CharUtils
extends java.lang.Object
This class tries to handle null
input gracefully.
An exception will not be thrown for a null
input.
Each method documents its behavior in more detail.
#ThreadSafe#
Modifier and Type | Field and Description |
---|---|
static char |
CR
Carriage return character CR ('\r', Unicode 000d).
|
static char |
LF
Linefeed character LF (
'\n' , Unicode 000a). |
static char |
NUL
null control character ('\0'), abbreviated NUL. |
Constructor and Description |
---|
CharUtils()
CharUtils instances should NOT be constructed in standard programming. |
Modifier and Type | Method and Description |
---|---|
static int |
compare(char x,
char y)
Compares two
char values numerically. |
static boolean |
isAscii(char ch)
Checks whether the character is ASCII 7 bit.
|
static boolean |
isAsciiAlpha(char ch)
Checks whether the character is ASCII 7 bit alphabetic.
|
static boolean |
isAsciiAlphaLower(char ch)
Checks whether the character is ASCII 7 bit alphabetic lower case.
|
static boolean |
isAsciiAlphanumeric(char ch)
Checks whether the character is ASCII 7 bit numeric.
|
static boolean |
isAsciiAlphaUpper(char ch)
Checks whether the character is ASCII 7 bit alphabetic upper case.
|
static boolean |
isAsciiControl(char ch)
Checks whether the character is ASCII 7 bit control.
|
static boolean |
isAsciiNumeric(char ch)
Checks whether the character is ASCII 7 bit numeric.
|
static boolean |
isAsciiPrintable(char ch)
Checks whether the character is ASCII 7 bit printable.
|
static char |
toChar(java.lang.Character ch)
Converts the Character to a char throwing an exception for
null . |
static char |
toChar(java.lang.Character ch,
char defaultValue)
Converts the Character to a char handling
null . |
static char |
toChar(java.lang.String str)
Converts the String to a char using the first character, throwing
an exception on empty Strings.
|
static char |
toChar(java.lang.String str,
char defaultValue)
Converts the String to a char using the first character, defaulting
the value on empty Strings.
|
static java.lang.Character |
toCharacterObject(char ch)
Deprecated.
Java 5 introduced
Character.valueOf(char) which caches chars 0 through 127. |
static java.lang.Character |
toCharacterObject(java.lang.String str)
Converts the String to a Character using the first character, returning
null for empty Strings.
|
static int |
toIntValue(char ch)
Converts the character to the Integer it represents, throwing an
exception if the character is not numeric.
|
static int |
toIntValue(java.lang.Character ch)
Converts the character to the Integer it represents, throwing an
exception if the character is not numeric.
|
static int |
toIntValue(java.lang.Character ch,
int defaultValue)
Converts the character to the Integer it represents, throwing an
exception if the character is not numeric.
|
static int |
toIntValue(char ch,
int defaultValue)
Converts the character to the Integer it represents, throwing an
exception if the character is not numeric.
|
static java.lang.String |
toString(char ch)
Converts the character to a String that contains the one character.
|
static java.lang.String |
toString(java.lang.Character ch)
Converts the character to a String that contains the one character.
|
static java.lang.String |
unicodeEscaped(char ch)
Converts the string to the Unicode format ' '.
|
static java.lang.String |
unicodeEscaped(java.lang.Character ch)
Converts the string to the Unicode format ' '.
|
public static final char LF
'\n'
, Unicode 000a).public static final char CR
public static final char NUL
null control character ('\0'), abbreviated NUL.public CharUtils()
CharUtils
instances should NOT be constructed in standard programming.
Instead, the class should be used as CharUtils.toString('c');
.
This constructor is public to permit tools that require a JavaBean instance to operate.
@Deprecated public static java.lang.Character toCharacterObject(char ch)
Character.valueOf(char)
which caches chars 0 through 127.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 convertpublic static java.lang.Character toCharacterObject(java.lang.String str)
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 convertpublic static char toChar(java.lang.Character ch)
null
.
CharUtils.toChar(' ') = ' ' CharUtils.toChar('A') = 'A' CharUtils.toChar(null) throws IllegalArgumentException
ch
- the character to convertjava.lang.NullPointerException
- if the Character is nullpublic static char toChar(java.lang.Character ch, char defaultValue)
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 nullpublic static char toChar(java.lang.String str)
CharUtils.toChar("A") = 'A' CharUtils.toChar("BA") = 'B' CharUtils.toChar(null) throws IllegalArgumentException CharUtils.toChar("") throws IllegalArgumentException
str
- the character to convertjava.lang.NullPointerException
- if the string is nulljava.lang.IllegalArgumentException
- if the String is emptypublic static char toChar(java.lang.String str, char defaultValue)
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 nullpublic static int toIntValue(char ch)
This method converts the char '1' to the int 1 and so on.
CharUtils.toIntValue('3') = 3 CharUtils.toIntValue('A') throws IllegalArgumentException
ch
- the character to convertjava.lang.IllegalArgumentException
- if the character is not ASCII numericpublic static int toIntValue(char ch, int defaultValue)
This method converts 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 numericpublic static int toIntValue(java.lang.Character ch)
This method converts the char '1' to the int 1 and so on.
CharUtils.toIntValue('3') = 3 CharUtils.toIntValue(null) throws IllegalArgumentException CharUtils.toIntValue('A') throws IllegalArgumentException
ch
- the character to convert, not nulljava.lang.NullPointerException
- if the Character is nulljava.lang.IllegalArgumentException
- if the Character is not ASCII numericpublic static int toIntValue(java.lang.Character ch, int defaultValue)
This method converts 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 numericpublic static java.lang.String toString(char ch)
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 convertpublic static java.lang.String toString(java.lang.Character ch)
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 convertpublic static java.lang.String unicodeEscaped(char ch)
This format is the Java source code format.
CharUtils.unicodeEscaped(' ') = " " CharUtils.unicodeEscaped('A') = "A"
ch
- the character to convertpublic static java.lang.String unicodeEscaped(java.lang.Character ch)
This format is the Java source code format.
If null
is passed in, null
will be returned.
CharUtils.unicodeEscaped(null) = null CharUtils.unicodeEscaped(' ') = " " CharUtils.unicodeEscaped('A') = "A"
ch
- the character to convert, may be nullpublic static boolean isAscii(char ch)
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 checkpublic static boolean isAsciiPrintable(char ch)
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 checkpublic static boolean isAsciiControl(char ch)
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 checkpublic static boolean isAsciiAlpha(char ch)
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 checkpublic static boolean isAsciiAlphaUpper(char ch)
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 checkpublic static boolean isAsciiAlphaLower(char ch)
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 checkpublic static boolean isAsciiNumeric(char ch)
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 checkpublic static boolean isAsciiAlphanumeric(char ch)
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 checkpublic static int compare(char x, char y)
char
values numerically. This is the same functionality as provided in Java 7.x
- the first char
to comparey
- the second char
to compare0
if x == y
;
a value less than 0
if x < y
; and
a value greater than 0
if x > y
Copyright © 2010 - 2023 Adobe. All Rights Reserved