Java program to remove vowels from string using StringBuffer class

Let’s learn java program to remove vowels from string using StringBuffer class.

Java program to remove vowels from string using StringBuffer class

Write a java program to remove the vowels from the string is the most common java interview question.

java program to remove vowels from string using StringBuffer class

In this post let’s learn to remove vowels from a string using StringBuffer class.

Also read – java program to remove consonants from a string

For example,

Input string: Hello World Java

Output string: Hll Wrld Jv

Input string: Hi how are you doing

Output string: H hw r y dng

Here’s java program to remove vowels from string.

// java program to remove vowels from a string using StringBuffer class
import java.util.Arrays;
import java.util.List;
public class RemoveVowels
{
   static String removeVowel(String strVowel)
   {
      Character[] vowel = {'a', 'e', 'i', 'o', 'u','A','E','I','O','U'};
      List<Character> li = Arrays.asList(vowel);
      StringBuffer strBuffer = new StringBuffer(strVowel);
      for(int a = 0; a < strBuffer.length(); a++)
      {
         if(li.contains(strBuffer.charAt(a)))
         {
            strBuffer.replace(a, a + 1, "") ;
            a--;
         }
      }
      return strBuffer.toString();
   }
   public static void main(String[] args)
   {
      String strInput = "Hello World Java";
      System.out.println(removeVowel(strInput));
   }
}


Output:

Hll Wrld Jv


Similarly we can write java program to remove vowels from a string using method.

public class RemoveVowelUsingMethod
{
   static String removeVowel(String strVowel) 
   { 
      return strVowel.replaceAll("[aeiouAEIOU]", ""); 
   }
   public static void main(String[] args) 
   {
      String strInput = "Flower Brackets";         
      System.out.println(removeVowel(strInput));
   }
}


Output:

Flwr Brckts


Also read – write a java program to remove the vowels from the string

Also read – java program to remove all vowels from a given string using switch case