Remove Vowels From String Java

Hello everyone!! Welcome to flower brackets blog. Let us learn how to remove vowels from string java.

remove vowels from string java

Let’s learn java program to delete vowels in a given string using replaceAll() function.

Remove vowels from string 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("String before removing vowels : " + actual);
      present = actual.replaceAll("[aeiouAEIOU]", " ");
      System.out.println("String after removing vowels : " + present);
      sc.close();
   }
}

Output :

Please enter a string : Deekshit Prasad
String before removing vowels: Deekshit Prasad
String after removing vowels: Dksht Prsd


Here’s the java program to remove all vowels from string using method,

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

public class VowelsInJava 
{
   static String removeVowel(String strVowel)
   {
      Character vowelsList[] = {'a', 'e', 'i', 'o', 'u','A','E','I','O','U'}; 
      List<Character> vo = Arrays.asList(vowelsList); 
      StringBuffer sb = new StringBuffer(strVowel); 

      for(int a = 0; a < sb.length(); a++) 
      { 
         if(vo.contains(sb.charAt(a)))
         {
            sb.replace(a, a + 1, "") ;
            a--;
         }
      }
      return sb.toString();
   }
 
   public static void main(String[] args) 
   {
      String strInput = "Welcome to Flower Brackets blog!!"; 
      System.out.println("Remove vowels from string java : " + removeVowel(strInput));
   }
}

Output :

Remove vowels from string java : Wlcm t Flwr Brckts blg!!


Do share this post if you like.

Related Posts