Remove Vowels from a String in Java
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.
Java Program
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
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.
- 1A for-each loop visits each character of the string in turn.
- 2
vowelSet.indexOf(c)checks whether the current character appears in"aeiouAEIOU". - 3Characters that don't appear in the vowel set are appended to a
StringBuilder; vowels are simply skipped. - 4The final
StringBuilderholds every original character except the vowels.
"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.
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
Approach 2: Regex Replace
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
Core Logic
A regex character class listing all five vowels, in both cases, removes every match in one call.
- 1
str.replaceAll("[aeiouAEIOU]", "")matches any character that is one of the ten listed vowel characters. - 2Every matched vowel is replaced with an empty string, which deletes it.
- 3What's left is a new string containing only the consonants and non-letters from the original.
"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.
Why: replaceAll() still has to scan the whole string and build a new one holding every kept character.
Key Concepts
Approach 3: Java 8
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
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.
- 1
str.chars()returns anIntStreamof the string's character codes. - 2
.filter(c -> vowelSet.indexOf(c) == -1)keeps only the codes that don't appear in the vowel set. - 3
.mapToObj(c -> String.valueOf((char) c))converts each surviving code back into a one-characterString. - 4
.collect(Collectors.joining())concatenates them all back into a single result string.
"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.
Why: The stream still visits every character once, and Collectors.joining() builds a result string holding every non-vowel character.