Java program to remove vowels from a string

Let us learn java program to remove vowels from a string.

java program to remove vowels from a string

To remove vowels from string java user enters a string as input. Then replaceAll() method replaces each substring of this string that matches given regular expression with the given replacement.

Also read – quicksort in java

import java.util.Scanner;

public class RemoveVowelsInString
{
   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 : Deekshit Prasad
Before removing vowels : Deekshit Prasad
After removing vowels : Dksht Prsd


Similarly we can remove all vowels from string using a method,

import java.util.Scanner;

public class VowelsInJava 
{
   static String removeVowel(String strVowel)
   {
      Character chVowels[] = {'a', 'e', 'i', 'o', 'u','A','E','I','O','U'};
      List<Character> li = Arrays.asList(chVowels);  
      StringBuffer sb = new StringBuffer(strVowel);
      for(int a = 0; a < sb.length(); a++) 
      {
         if(li.contains(sb.charAt(a)))
         {
            sb.replace(a, a + 1, "");
            a--;
         }
      }
      return sb.toString();
   }

   public static void main(String[] args) 
   {
      String strInput = "Flower Brackets";
      System.out.println(removeVowel(strInput));
   }
}

Output :

Flwr Brckts

Related Posts