Home
Map
Character ExamplesUse the Character class to test and transform chars. Call isLetter, isDigit and toLowerCase.
Java
This page was last reviewed on May 24, 2023.
Character. In Java a char is a value, much like an int. Often in programs we need to test for ranges (classes) of characters—as for digits, letters, spaces.
Character notes. Instead of acting upon chars directly, we can use built-in methods. The character class includes this logic—we just access it.
Character class. Enter the Character class. With the static methods found here, we analyze and transform chars, with little (or no) custom code.
static
Character.isDigit Character.isLetter Character.isLetterOrDigit Character.isLowerCase Character.isUpperCase Character.isWhitespace Character.toLowerCase Character.toUpperCase
IsDigit. This method returns true if the char passed to it is "0" through "9." It also supports Unicode so is more complex (and likely slower) than an if-statement and range check.
Here The value 9 is considered a digit. But the values Y and the space character are not digits. This makes sense.
public class Program { public static void main(String[] args) { char value1 = '9'; char value2 = 'Y'; char value3 = ' '; // See if these characters are digits. System.out.println(Character.isDigit(value1)); System.out.println(Character.isDigit(value2)); System.out.println(Character.isDigit(value3)); } }
true false false
IsLetter, for-loop. With Character.isLetter we detect lowercase and uppercase letters. Digits, spaces and punctuation are not considered letters. IsLetter returns true or false.
Detail We use the charAt method in this program to access each char in the String. We then test with isLetter.
public class Program { public static void main(String[] args) { String value = "abc123"; // Loop through characters in this String. for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); // See if the character is a letter or not. if (Character.isLetter(c)) { System.out.println(c + " = LETTER"); } else { System.out.println(c); } } } }
a = LETTER b = LETTER c = LETTER 1 2 3
IsLetterOrDigit. In my experience, this method is often useful. Most tokens are composed of letters and digits. So with isLetterOrDigit we can easily skip past punctuation and spaces.
public class Program { public static void main(String[] args) { char value1 = 'U'; char value2 = '9'; char value3 = 'w'; char value4 = '*'; System.out.println(Character.isLetterOrDigit(value1)); System.out.println(Character.isLetterOrDigit(value2)); System.out.println(Character.isLetterOrDigit(value3)); System.out.println(Character.isLetterOrDigit(value4)); // [False] } }
true true true false
IsLowerCase, isUpperCase. These return true or false if the character is lowercase (or uppercase). This can be implemented (for ASCII) with a simple if-check, but that is harder to read.
However For performance work, using an if-check may be better. This is something that needs benchmarking.
public class Program { public static void main(String[] args) { char[] values = { 'C', 'a', 't', '5' }; // See if the chars are lower or uppercase. for (char value : values) { System.out.println(value); System.out.println(Character.isLowerCase(value)); System.out.println(Character.isUpperCase(value)); } } }
C false true a true false t true false 5 false false
IsWhitespace. This method is a new version of Character.isSpace. It returns true if the character is whitespace (like a space, tab, or newline). It returns false on other chars.
Tip For methods that skip past non-word chars, Character.isWhitespace is better than a complex if-test: it is simpler and easier to read.
public class Program { public static void main(String[] args) { // Test these characters for whitespace. System.out.println(Character.isWhitespace(' ')); System.out.println(Character.isWhitespace('\n')); System.out.println(Character.isWhitespace('Q')); } }
true true false
Invert case. More complex logic is possible. This program applies toLowerCase and toUpperCase to invert the casing of letters in a String.
So If a char is uppercase, it is changed to lowercase, and the opposite. It uses isUpperCase and isLowerCase to test chars.
Detail The invertCase method gets a mutable char array with toCharArray. It tests and changes chars. It returns a new String.
public class Program { static String invertCase(String value) { // Convert to a char array. char[] array = value.toCharArray(); for (int i = 0; i < array.length; i++) { // Change uppercase letters to lowercase ones. // ... And change lower to upper. if (Character.isUpperCase(array[i])) { array[i] = Character.toLowerCase(array[i]); } else if (Character.isLowerCase(array[i])) { array[i] = Character.toUpperCase(array[i]); } } return new String(array); } public static void main(String[] args) { // Use invertCase method. System.out.println(invertCase("Dog")); System.out.println(invertCase(invertCase("Cat"))); } }
dOG Cat
ASCII table. With the Character class, we can generate a table of all 128 ASCII values. Programs can manipulate ASCII values to improve performance.
ASCII Table
Lookup table. For optimization, we can store character transformations in a table. This reduces any character modification to a simple array element access.
char Lookup Table
Handle sentence characters. One algorithm that uses isLetter and toLowerCase is a palindrome-detecting algorithm. It skips over whitespace and non-letter chars to detect palindromes.
Palindrome
Character-based algorithms are often faster than String-based ones. Fewer allocations are needed: a char is simpler than an entire String. With the Character class, we test char types.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.