Check if a string contains only alphabets in Java using ASCII values

Let’s say we have set out inut string in myStr variable. Now loop through until the length of string and check for alphabets with ASCII values −

for (int i = 0; i = 'A' && c = 'a' && c 

Following is an example to check if a string contains only alphabets in Java using ASCII values;

Example

class Main {
   public static boolean checkAlphabet(String myStr) {
      for (int i = 0; i = 'A' && c = 'a' && c 

Output

String1 = Tom1
Is String1 contains only alphabets? = false
String2 = Tim
Is String2 contains only alphabets? = true
Updated on: 2019-09-20T08:29:26+05:30

524 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements