
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can a String be validated (for alphabets) in java?
Problem Statement
The given problem is to validate a string to check if that string contains only alphabets (both uppercase and lowercase) and does not contains any other characters like numbers, special characters, etc.. For example, the string "HelloWorld" is valid as it contains only alphabets, while "Hello123" is not valid as it contains numbers.
Solution
We can solve this problem by using following methods:
Using Regular Expressions
We can use regular expressions to validate a string for alphabets. The regular expression ^[a-zA-Z]+$
matches a string that contains only uppercase and lowercase letters.
Let's look at the steps:
- Define a regular expression pattern
^[a-zA-Z]+$
. - Compile the pattern using Pattern.compile().
- Create a Matcher object using the compiled pattern and the input string.
- Now, Use the matches() method of the Matcher object to check if the string matches the pattern.
- Return true if it matches, otherwise return false.
Example
Following is the Java program to validate a string for alphabets using regular expressions:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class ValidateStringIsAlphabetic { public static void main(String[] args){ String s = "HelloWo232"; boolean isValid = validateString(s); if(isValid){ System.out.println("The string is valid and contains only alphabets."); } else { System.out.println("The string is not valid. It contains characters other than alphabets."); } } public static boolean validateString(String str){ String regex = "^[a-zA-Z]+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); return matcher.matches(); } }
When the above code is executed, it produces the following output:
The string is not valid. It contains characters other than alphabets.
Manual Checking using loop
We can manually check whether given string is a proper name by comparing each character in it with the characters in the English alphabet. Following are the steps:
- Get the string to be validated.
- Convert the string to a character array using toCharArray().
- Loop through each character in the array.
- Check if each character is within the range of 'a' to 'z' or 'A' to 'Z'.
- If any character is not in the range, return false.
Example
Following is the Java program to validate a string for alphabets using manual checking:
public class ValidateStringIsAlphabetic { public static void main(String[] args){ String s = "HelloWorld"; boolean isValid = validateString(s); if(isValid){ System.out.println("The string is valid and contains only alphabets."); } else { System.out.println("The string is not valid. It contains characters other than alphabets."); } } public static boolean validateString(String str){ char[] chars = str.toCharArray(); for(char c : chars){ if(!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))){ return false; } } return true; } }
When the above code is executed, it produces the following output:
The string is valid and contains only alphabets.
Using Character.isAlphabetic()
Another way to validate a string for alphabets is to use the Character.isAlphabetic() method. This method checks if a character is an alphabetic character.
Follow the steps below:
- Convert the string to a character array using toCharArray().
- Loop through each character in the array.
- Use Character.isAlphabetic() to check if each character is an alphabetic character.
- If any character is not alphabetic, return false.
Example
Following is the Java program to validate a string for alphabets using Character.isAlphabetic():
import java.lang.Character; public class ValidateStringIsAlphabetic { public static void main(String[] args){ String s = "HelloWorld"; boolean isValid = validateString(s); if(isValid){ System.out.println("The string is valid and contains only alphabets."); } else { System.out.println("The string is not valid. It contains characters other than alphabets."); } } public static boolean validateString(String str){ char[] chars = str.toCharArray(); for(char c : chars){ if(!Character.isAlphabetic(c)){ return false; } } return true; } }
When the above code is executed, it produces the following output:
The string is valid and contains only alphabets.