Remove Vowels From String Java

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

Well, you will be learning different methods in removing vowel from a string. Below are few methods to remove vowels from string with example,

Remove Vowels From String Java

Let’s see remove vowels from string java,

Remove Vowels From String Java

Method 1: let’s learn java remove vowels from string using replaceAll() function.

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

Output:

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


Method 2: here in this method we will be calling delete_Vowel() function from the main method.

Let’s see the code on how to remove vowels from a string in java,

Also Read – Java Example To Print All Alphabets

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 = deleteVowel(actual);
      System.out.println("String after removing vowels: " + present);
   }

   private static String deleteVowel(String x)
   {
      String strModified = "";
      int m;

      for(m = 0; m < x.length(); m++)
      {
         if(!checkVowel(Character.toLowerCase(x.charAt(m))))
         {
            strModified = strModified + x.charAt(m);
         }
      }
      return strModified;
   }

   private static boolean checkVowel(char y)
   {
      String vowels = "aeiou";
      for(int n = 0; n < 5; n++)
      {
         if(y == vowels.charAt(n))
         {
            return true;
         }
      }
      return false;
   }
}

Output:

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


Method 3: last but not the least, in this method we are going to use for loop to remove vowels in the given(entered) string.

Let’s see how to write a java program to remove the vowels from the string,

Also Read – Java Program To Find GCD And LCM Of Two Numbers

import java.util.Scanner;

public class RemoveVowelsInString
{
   public static void main(String[] args)
   {
      System.out.print("Please enter a string : ");
      Scanner sc = new Scanner(System.in);
      String actual = sc.nextLine();
      System.out.println("String before removing vowels : " + actual);
      char word[] = actual.toCharArray();
      char modifiedWord[] = new char[80];
      int y = 0;
 
      for(int x = 0; x < word.length; x++)
      {
         if(word[x] == 'a' || word[x] == 'e' || word[x] == 'i' || word[x] == 'o' || word[x] == 'u')
         {
            continue;
         }
         else
         {
            modifiedWord[y] = word[x];
            y++;
         }
      }
      System.out.print("String after removing vowels: ");
      System.out.print(modified_Word);
   }
}

Output:

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


using java util.list

In this segment let us learn remove vowels from a string java using java util.list,

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!!


using switch case statement

Here let us learn how to write a program to remove the vowels from the input string in java using switch case statement,

public class RemoveVowels 
{
   public static boolean checkVowel(char ch)
   {
      switch(ch)
      {
         case 'A': 
         case 'E': 
         case 'I':
         case 'O':
         case 'U':
         case 'a':
         case 'e': 
         case 'i':
         case 'o':
         case 'u':
            return true;
         default:
            return false; 
      }
   }
 
   public static void main(String[] args) 
   {
      String str1 = "Welcome to Flower Brackets blog!!";
      String str2 = "";
      char vowel;
 
      System.out.println("Before remove vowels from string : " + str1);
 
      for(int a = 0; a < str1.length(); a++)
      {
         vowel = str1.charAt(a);
         if(!checkVowel(vowel))
         {
            str2 += vowel;
         }
      } 
      System.out.println("After remove vowels from string : " + str2);
   }
}

Output:

Before remove vowels from string : Welcome to Flower Brackets blog!!
After remove vowels from string : Wlcm t Flwr Brckts blg!!


Using StringBuffer class : Now let’s learn java remove vowels from input string using StringBuffer class,

import java.util.Scanner;

public class RemoveVowelJava 
{
   public static String removeVowel(String str) 
   {
      StringBuffer sb = new StringBuffer();
      for(int a = 0; a < str.length(); a++)
         if((a % 2) == 0)
            sb.append(str.charAt(a));
         else if((a % 2) != 0)
            if(str.charAt(a) != 'a' && str.charAt(a) != 'e' && str.charAt(a) != 'i' && str.charAt(a) != 'o' && str.charAt(a) != 'u')
               if(str.charAt(a) != 'A' && str.charAt(a) != 'E' && str.charAt(a) != 'I' && str.charAt(a) != 'O' && str.charAt(a) != 'U')
                  sb.append(str.charAt(a));
         return sb.toString();
   }
 
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a sentence to remove vowels : ");
      String strInput = sc.nextLine(); 
      System.out.println("After remove vowels from string : " + removeVowel(strInput));
      sc.close();
   }
}

Output:

Please enter a sentence to remove vowels : Welcome to Flower Brackets blog!!
After remove vowels from string : Wlcome t Flwr Brackts blg!!


conclusion

That’s it guys. This was all about program to remove vowels from a string in java.

I hope you guys have understood java program to remove vowels from a string concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like