Java program to remove/delete vowels from a string

Hey guys!! Welcome to flower brackets blog. In this article we are going to learn java program to remove/delete vowels from a string.

Java program to remove/delete vowels from a string

Here’s the java program on how to remove all vowels from string in java,

Also read – remove vowels from string using switch case

import java.util.Scanner;

public class RemoveVowelJava
{
   public static void main(String[] args)
   {
      String actual;
      String present;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string : ");
      actual = sc.nextLine();
      System.out.println("Before removing vowels : " + actual);
      present = actual.replaceAll("[aeiouAEIOU]", " ");
      System.out.println("After removing vowels : " + present);
      sc.close();
   }
}

Output:

Please enter a string : flower brackets
Before removing vowels : flower brackets
After removing vowels : fl w r br ck ts

Similarly above java program can be executed using StringBuffer class,

import java.util.Arrays;
import java.util.List;

public class RemoveVowelsInString 
{
   static String removeVowel(String strVowel) 
   { 
      Character vowel[] = {'a', 'e', 'i', 'o', 'u','A','E','I','O','U'};   
      List 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

Related Posts