Filter Only Alphabets
This program filters only alphabets from string.
Problem Statement
Write a Java program to filter only alphabets from "Eduinq123@Test".
Source Code
| 1 | public class RegexFilterAlphabets { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Regex Filter Alphabets"); |
| 4 | String str="Eduinq123@Test"; |
| 5 | String result=str.replaceAll("[^A-Za-z]",""); |
| 6 | System.out.println("Filtered string: "+result); |
| 7 | } |
| 8 | } |
Program Output
Eduinq Regex Filter Alphabets Filtered string: EduinqTest
Explanation
The character class [^A-Za-z] matches any character that is not a letter, and replaceAll() removes all such characters, demonstrating regex-based filtering to keep only alphabetic characters.