TheDeveloperBlog.com


Java Character: isLetter, isDigit and toLowerCase

Character. 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.

Int

This is repetitive. Using custom if-checks in each program is also prone to errors: what if you mistype a single character? The program will fail.


Character class. Enter the Character class. With the static methods found here, we analyze and transform chars, with little (or no) custom code.

Static Method
Methods

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.

Java program that uses Character.isDigit

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));
    }
}

Output

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.

CharAt: We use the charAt method in this program to access each char in the String. We then test with isLetter.

Java program that uses Character.isLetter

public class Program {
    public static void main(String[] args) {

	String value = "R2D2";
	// 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);
	    }
	}
    }
}

Output

R = LETTER
2
D = LETTER
2

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.

Java program that uses isLetterOrDigit

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]
    }
}

Output

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.

Java program that uses isLowerCase, isUpperCase

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));
	}
    }
}

Output

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.

Java program that uses Character.isWhitespace

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'));
    }
}

Output

true
true
false

ToLowerCase, toUpperCase invert case. 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.

InvertCase: The invertCase method gets a mutable char array with toCharArray. It tests and changes chars. It returns a new String.

Java program that uses toLowerCase, toUpperCase

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")));
    }
}

Output

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

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.