In this post, we will write a Java program to check if the String contains only Unicode letters.
This program uses the Character.isLetter() method to check if the character is a letter or not.
This program uses the Character.isLetter() method to check if the character is a letter or not.
How to Check if the String Contains Only Letters
Let's write a Java method to check if the given String contains only letters or not:
public static boolean isAlpha(final CharSequence cs) { if (isEmpty(cs)) { return false; } final int sz = cs.length(); for (int i = 0; i < sz; i++) { if (!Character.isLetter(cs.charAt(i))) { return false; } } return true; } public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; }
Let's write a complete Java program with JUnit test cases to test the above method:
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; /** * Checks if the CharSequence contains only Unicode letters * @author javaguides.net * */ public class IsAlphaExample { public static boolean isAlpha(final CharSequence cs) { if (isEmpty(cs)) { return false; } final int sz = cs.length(); for (int i = 0; i < sz; i++) { if (!Character.isLetter(cs.charAt(i))) { return false; } } return true; } public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; } @Test public void testIsAlpha() { assertFalse(isAlpha(null)); assertFalse(isAlpha("")); assertFalse(isAlpha(" ")); assertTrue(isAlpha("a")); assertTrue(isAlpha("A")); assertTrue(isAlpha("kgKgKgKgkgkGkjkjlJlOKLgHdGdHgl")); assertFalse(isAlpha("ham kso")); assertFalse(isAlpha("1")); assertFalse(isAlpha("hkHKHik6iUGHKJgU7tUJgKJGI87GIkug")); assertFalse(isAlpha("_")); assertFalse(isAlpha("hkHKHik*khbkuh")); } }
Related Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Swap Two Strings Without Using Third Variable
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Swap Two Strings Without Using Third Variable
Comments
Post a Comment
Leave Comment