remove vowels from string java program code example

Example 1: Java program to remove vowels from string using StringBuffer class

// 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));
   }
}

Example 2: Java program to delete vowels in a given string

// Java program to delete vowels in a given string
import java.util.*;
public class RemoveVowelsInString
{
   public static void main(String[] args)
   {
      String str = "Deekshit Prasad";
      System.out.println("Given string: " + str);
      str = str.replaceAll("[AaEeIiOoUu]", "");
      System.out.println("After deleting vowels in given a string: " + str);
   }
}