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 remove vowels in a given string using replaceAll() method.

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 manual method,

import java.util.Scanner;

public class VowelsInJava 
{
   static String removeVowel(String strVowel)
   {
      String str = "";
      for(int a = 0; a < strVowel.length(); a++)
      {
         if(!isVowel(Character.toLowerCase(strVowel.charAt(a))))
         {
            str = str + strVowel.charAt(a);
         }
      }
      return str;
   }

   private static boolean isVowel(char ch)
   {
      String vowel = "aeiou";
      for(int a = 0; a < 5; a++)
      {
         if(ch == vowel.charAt(a))
         {
            return true;
         }
      }
      return false;
   }

   public static void main(String[] args) 
   {
      String strInput, strOutput;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter a string : ");
      strInput = sc.nextLine();
      strOutput = removeVowel(strInput);
      System.out.print("After removing vowels : " + strOutput);
      sc.close();
   }
}

Output :

Please enter a string : Deekshit Prasad
After removing vowels : Dksht Prsd

Related Posts