Java ProgramsStringsRemove Vowels from a String

Remove Vowels from a String in Java

beginner·  Strings  ·  String Manipulation

Problem

Vowels are the letters a, e, i, o, and u — every other letter is a consonant.

Given a string, remove every vowel it contains.

Input
Programming
Output
Prgrmmng

Java Program

Java
public class RemoveVowels { public static void main(String[] args) { String str = "Programming"; String vowelSet = "aeiouAEIOU"; StringBuilder result = new StringBuilder(); for (char c : str.toCharArray()) { if (vowelSet.indexOf(c) == -1) result.append(c); // skip vowels entirely } System.out.println(result.toString()); } }

Output

Prgrmmng

Core Logic

Keeping every character that isn't in a small set of vowels, one pass through the string, filters out the vowels and leaves every consonant in place.

How It Works
  1. 1A for-each loop visits each character of the string in turn.
  2. 2vowelSet.indexOf(c) checks whether the current character appears in "aeiouAEIOU".
  3. 3Characters that don't appear in the vowel set are appended to a StringBuilder; vowels are simply skipped.
  4. 4The final StringBuilder holds every original character except the vowels.
For "Programming", the vowels 'o', 'a', and 'i' are skipped, producing "Prgrmmng".
💡

Key Point: This mirrors the vowel-counting program exactly, just appending non-vowels to a result instead of incrementing a counter for vowels.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each character is visited once and the result buffer grows to hold every non-vowel character, which can be up to n.

Key Concepts

String.indexOf()StringBuilderfor-each loop

Approach 2: Regex Replace

Java
public class RemoveVowelsRegex { public static void main(String[] args) { String str = "Programming"; // Matches and removes every vowel character, in both cases String result = str.replaceAll("[aeiouAEIOU]", ""); System.out.println(result); } }

Output

Prgrmmng

Core Logic

A regex character class listing all five vowels, in both cases, removes every match in one call.

How It Works
  1. 1str.replaceAll("[aeiouAEIOU]", "") matches any character that is one of the ten listed vowel characters.
  2. 2Every matched vowel is replaced with an empty string, which deletes it.
  3. 3What's left is a new string containing only the consonants and non-letters from the original.
Replacing every vowel in "Programming" removes 'o', 'a', and 'i', producing "Prgrmmng".
💡

Key Point: Unlike the negated character class used to strip special characters, this one lists the vowels directly, since there are only ten to match against.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: replaceAll() still has to scan the whole string and build a new one holding every kept character.

Key Concepts

regexreplaceAll()

Approach 3: Java 8

Java
import java.util.stream.Collectors; public class RemoveVowelsStream { public static void main(String[] args) { String str = "Programming"; String vowelSet = "aeiouAEIOU"; // Keeps every character that isn't a vowel, then joins them back into a String String result = str.chars() .filter(c -> vowelSet.indexOf(c) == -1) .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.joining()); System.out.println(result); } }

Output

Prgrmmng

Core Logic

The same is-not-a-vowel check can filter a stream of character codes, then join what's left back into a string.

How It Works
  1. 1str.chars() returns an IntStream of the string's character codes.
  2. 2.filter(c -> vowelSet.indexOf(c) == -1) keeps only the codes that don't appear in the vowel set.
  3. 3.mapToObj(c -> String.valueOf((char) c)) converts each surviving code back into a one-character String.
  4. 4.collect(Collectors.joining()) concatenates them all back into a single result string.
Filtering "Programming" drops every vowel, and joining what's left reassembles "Prgrmmng".
💡

Key Point: This is the mirror image of the vowel-counting stream pipeline — same filter condition, but joining the survivors into a string instead of counting them.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The stream still visits every character once, and Collectors.joining() builds a result string holding every non-vowel character.

Key Concepts

Streamchars()filter()Collectors.joining()

Related Programs