These functions deal only with character manipulations. Some of them are explained below:
Method | Description |
---|---|
isLetter(char ch) |
Determines whether the specified character is a letter or not. |
isDigit(char ch) |
Determines whether the specified character is a digit or not. |
isWhitespace(char ch) |
Determines whether the specified character is whitespace or not. |
toUpperCase(char ch) |
Converts the specified character to uppercase. |
toLowerCase(char ch) |
Converts the specified character to lowercase. |
toString(char ch) |
Returns a string representation of the specified character. |
compareTo(char c1, char c2) |
Compares two characters numerically. |
Here is an example of how to use these methods in Java:
char ch1 = 'a';
char ch2 = '1';
if (Character.isLetter(ch1)) {
System.out.println(ch1 + ” is a letter”);
} else {
System.out.println(ch1 + ” is not a letter”);
}
if (Character.isDigit(ch2)) {
System.out.println(ch2 + ” is a digit”);
} else {
System.out.println(ch2 + ” is not a digit”);
}
System.out.println(“The uppercase of “ + ch1 + ” is “ + Character.toUpperCase(ch1));
System.out.println(“The lowercase of “ + ch1 + ” is “ + Character.toLowerCase(ch1));
System.out.println(“The string representation of “ + ch1 + ” is “ + Character.toString(ch1));
System.out.println(“The numerical comparison of “ + ch1 + ” and “ + ch2 + ” is “ + Character.compare(ch1, ch2));
Output:
a is a letter
1 is a digit
The uppercase of a is A
The lowercase of a is a
The string representation of a is a
The numerical comparison of a and 1 is 32
In this example, we use the isLetter()
method to check whether the character ch1
is a letter, and the isDigit()
method to check whether the character ch2
is a digit. We also use the toUpperCase()
and toLowerCase()
methods to convert the character ch1
to uppercase and lowercase, respectively. The toString()
method is used to convert the character ch1
to a string, and the compare()
method is used to compare the characters ch1
and ch2
numerically.