SwadhyAI Notes

All Java notes

How to Remove Vowels from a String in Java

The Challenge

Remove all vowels (a, e, i, o, u) from a string!

Solution

Method 1: Using replaceAll()

java
String text = "Hello World";
String result = text.replaceAll("[aeiouAEIOU]", "");

System.out.println("Original: " + text);
System.out.println("Without vowels: " + result);
// Output: Original: Hello World
//        Without vowels: Hll Wrld

Method 2: Using StringBuilder

java
String text = "Hello World";
StringBuilder result = new StringBuilder();
String vowels = "aeiouAEIOU";

for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    if (vowels.indexOf(c) == -1) {
        result.append(c);
    }
}

System.out.println("Without vowels: " + result.toString());
// Output: Without vowels: Hll Wrld

Method 3: Using Java 8 Streams

java
String text = "Hello World";
String result = text.chars()
                    .filter(c -> "aeiouAEIOU".indexOf(c) == -1)
                    .collect(StringBuilder::new,
                            StringBuilder::appendCodePoint,
                            StringBuilder::append)
                    .toString();

System.out.println("Without vowels: " + result);
// Output: Without vowels: Hll Wrld

Did You Know?

The regex pattern [aeiouAEIOU] matches any single vowel character. The square brackets create a character class!

Pro Tip

The replaceAll() method is the simplest and most readable solution for this task. Use it unless you have specific performance requirements!

Real-World Use Cases

  • Text compression: Reduce text size
  • Word games: Create puzzles or challenges
  • Data sanitization: Clean up input
  • Cryptography: Simple text obfuscation
›Short versionOpen if you only have a minute.
  • Build a new string. Strings are immutable, so the original stays.
  • Append kept characters to a StringBuilder. result += c copies the whole buffer every time.
  • That copy is O(n^2). One linear pass already does the job.
  • Decide case and whether y counts before the loop. Whitespace is not a vowel.

Remove vowels

Build a new string containing every character except vowels.

Working approach

  • Walk each char once.
  • Append it when it is not a vowel under the case rule you chose.
  • Return builder.toString() at the end.

Walk the sample

  1. Input "Java"
  2. J stays, a is dropped, v stays, a is dropped
  3. The result is "Jv"

The trap

Concatenating strings inside the loop is the quadratic version of a linear filter.

java
static String removeVowels(String text) {
    StringBuilder kept = new StringBuilder(text.length());
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        char lower = Character.toLowerCase(c);
        if ("aeiou".indexOf(lower) < 0) {
            kept.append(c);
        }
    }
    return kept.toString();
}

Why is kept += c inside the loop O(n^2)?

Each String concatenation copies every character already kept. Across n appends that is about n^2 / 2 character copies.

StringBuilder.append is amortized constant time per character, so the filter is linear. A regex that forgets uppercase also deletes the wrong set, which is a separate bug.

Time O(n), space O(n) for the result.

Easy to get wrong

  • Using += on a String inside the loop.
  • Deleting every character a loose pattern happens to match.