Java Online Compiler
Example: Remove Non-Alphabets by Iteration in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Remove Non-Alphabets by Iteration public class Main { public static void main(String[] args) { // Step 1: Define the input string String originalString = "Learn Java @ CodeAcademy!"; System.out.println("Original String: " + originalString); // Step 2: Create a StringBuilder to efficiently build the new string StringBuilder cleanedStringBuilder = new StringBuilder(); // Step 3: Iterate through each character of the original string for (char c : originalString.toCharArray()) { // Step 4: Check if the character is a letter if (Character.isLetter(c)) { // Step 5: Append the letter to the StringBuilder cleanedStringBuilder.append(c); } } // Step 6: Convert the StringBuilder content back to a String String cleanedString = cleanedStringBuilder.toString(); // Step 7: Print the cleaned string System.out.println("Cleaned String: " + cleanedString); } }
Output
Clear
ADVERTISEMENTS