Check if a string contains only alphabets in Java using Lambda expression
Given a String, the task is to check whether a string contains only alphabets or not.
Examples:
Input : GeeksforGeeks Output : True Input : Geeks4Geeks Output : False Input : null Output : False
The idea is to use isLetter() method of Character class.
Algorithm:
- Get the string
- Match the string:
- Check if the string is empty or not. If empty, return false
- Check if the string is null or not. If null, return false.
- If the string is neither empty nor null,
then check using Lambda Expression Character::isLetter().
- Return true if matched
Pseudocode:
public static boolean isStringOnlyAlphabet(String str) { return ((!str.equals("")) && (str != null) && (str.chars().allMatch(Character::isLetter))); } |
chevron_right
filter_none
Program: Checking if String contains only Alphabets.
// Java program to check if String contains only Alphabets // using Lambda Expression class GFG { // Function to check String for only Alphabets public static boolean isStringOnlyAlphabet(String str) { return ((str != null) && (!str.equals("")) && (str.chars().allMatch(Character::isLetter))); } // Main method public static void main(String[] args) { // Checking for True case System.out.println("Test Case 1:"); String str1 = "GeeksforGeeks"; System.out.println("Input: " + str1); System.out.println("Output: " + isStringOnlyAlphabet(str1)); // Checking for String with numeric characters System.out.println("\nTest Case 2:"); String str2 = "Geeks4Geeks"; System.out.println("Input: " + str2); System.out.println("Output: " + isStringOnlyAlphabet(str2)); // Checking for null String System.out.println("\nTest Case 3:"); String str3 = null; System.out.println("Input: " + str3); System.out.println("Output: " + isStringOnlyAlphabet(str3)); // Checking for empty String System.out.println("\nTest Case 4:"); String str4 = ""; System.out.println("Input: " + str4); System.out.println("Output: " + isStringOnlyAlphabet(str4)); } } |
chevron_right
filter_none
Output:
Test Case 1: Input: GeeksforGeeks Output: true Test Case 2: Input: Geeks4Geeks Output: false Test Case 3: Input: null Output: false Test Case 4: Input: Output: false
Check if a string contains only alphabets in Java using ASCII values
Check if a string contains only alphabets in Java using Regex
Recommended Posts:
- Java Lambda Expression with Collections
- Check if a string contains only alphabets in Java using Regex
- Check if a string contains only alphabets in Java using ASCII values
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Map function and Lambda expression in Python to replace characters
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Lambda Expressions in Java 8
- Check if words are sorted according to new order of alphabets
- Remove all characters other than alphabets from string
- Sub-string that contains all lowercase alphabets after performing the given operation
- Check for balanced parentheses in an expression
- Check for balanced parentheses in an expression | O(1) space
- Solve the Logical Expression given by string
- Evaluate a boolean expression represented as string
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



