Java Online Compiler
Example: Remove Non-Alphabets using replaceAll in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Remove Non-Alphabets using replaceAll public class Main { public static void main(String[] args) { // Step 1: Define the input string String originalString = "Java123 Programming! is #Fun."; System.out.println("Original String: " + originalString); // Step 2: Use replaceAll() with a regular expression // [^a-zA-Z] matches any character that is NOT an alphabet (a-z or A-Z) String cleanedString = originalString.replaceAll("[^a-zA-Z]", ""); // Step 3: Print the cleaned string System.out.println("Cleaned String: " + cleanedString); } }
Output
Clear
ADVERTISEMENTS