Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How do we remove all the vowels from a string?



Let’s learn how to remove all the vowels in a string. We’ll input the string "hello educative" into our algorithm. After this, our output should be "hll dctv".


Vowels are a limited set of characters in the English language. The characters “a”, “e”, “i”, “o”, and “u” are considered vowels.

We’ll use the following algorithm to remove vowels from a string:

  1. Define a set containing all the vowels.

  2. Loop over the given string.

  3. For every character, we’ll check to see if it is present in the set of vowels.

    a. If the character is present in the set, we’ll remove the character from the string.

    b. If the character is not present in the vowel set, we’ll continue iterating over the string.


import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Main{

    public static void main(String[] args) {
        String text = "hello educative";
        StringBuilder stringBuilder = new StringBuilder();
        Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i','o','u'));
        for(char ch: text.toLowerCase().toCharArray()){
            if(!vowels.contains(ch)) stringBuilder.append(ch);
        String textWithoutVowels = stringBuilder.toString();
        System.out.println("Original Text - " + text);
        System.out.println("Text without vowels - " + textWithoutVowels);


  • Lines 1–3: We import the relevant libraries.
  • Line 8: We define a string called text.
  • Line 9: We define a StringBuilder instance.
  • Line 10: We define a HashSet called vowels that contains all the vowels.
  • Lines 11–13: We loop through all the characters of the text string using a for loop. In the loop, we check if the set vowels contains the current character. If the character is not in the vowels set, we add it to the StringBuilder instance. Otherwise, we reject the character.
  • Line 14: We obtain a string called textWithoutVowels from the StringBuilder instance.
  • Lines 15–16: We print the text string and the textWithoutVowels string to the console.



View all Courses

Keep Exploring