Java Online Compiler
Example: Remove Vowels using StringBuilder in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Remove Vowels using StringBuilder import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Define the input string String originalString = "Java Development"; System.out.println("Original String: " + originalString); // Step 2: Initialize a StringBuilder to efficiently build the new string StringBuilder sb = new StringBuilder(); // Step 3: Define a string containing all vowels for quick lookup String vowels = "aeiouAEIOU"; // Step 4: Iterate through each character of the original string for (int i = 0; i < originalString.length(); i++) { char ch = originalString.charAt(i); // Step 5: Check if the character is a vowel // If the character's index in the 'vowels' string is -1, it's not a vowel. if (vowels.indexOf(ch) == -1) { sb.append(ch); // Append non-vowel characters to StringBuilder } } // Step 6: Convert StringBuilder to String and print the result String stringWithoutVowels = sb.toString(); System.out.println("String without Vowels: " + stringWithoutVowels); // Example with user input Scanner scanner = new Scanner(System.in); System.out.print("\nEnter another string: "); String userInput = scanner.nextLine(); StringBuilder userSb = new StringBuilder(); for (int i = 0; i < userInput.length(); i++) { char ch = userInput.charAt(i); if (vowels.indexOf(ch) == -1) { userSb.append(ch); } } System.out.println("Processed String: " + userSb.toString()); scanner.close(); } }
Output
Clear
ADVERTISEMENTS