C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Methods
Character.isDigit
Character.isLetter
Character.isLetterOrDigit
Character.isLowerCase
Character.isUpperCase
Character.isWhitespace
Character.toLowerCase
Character.toUpperCase
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
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 = "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);
}
}
}
}
Output
a = LETTER
b = LETTER
c = LETTER
1
2
3
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
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
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
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