TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Character Examples

Use the Character class to test and transform chars. Call isLetter, isDigit and toLowerCase.
Character. In ancient times, humans made cave paintings. There were no letters or numbers. But today we have words and characters.
Character notes. 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 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 = "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
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
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.

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
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf