Java program to delete vowels in a given string

Let’s learn java program to delete vowels in a given string.

Java program to delete vowels in a given string

Write a java program to remove the vowels from the string or write a program to remove the vowels from the input string in java is the most asked interview question.

java program to delete vowels in a given string

In program to remove all vowels from a given string in java user enters a string as input.

Also read – java program to print multiplication table of any number

Then replaceAll() method replaces each substring of this string that matches given regular expression with the given replacement.

Here’s program to delete vowels in a given string.

import java.util.*;
public class RemoveVowelsInString
{
   public static void main(String[] args)
   {
      String str = "Deekshit Prasad";
      System.out.println("Given string: " + str);
      str = str.replaceAll("[AaEeIiOoUu]", "");
      System.out.println("After deleting vowels in given a string: " + str);
   }
}


Output:

Given string: Deekshit Prasad
After deleting vowels in a given string: Dksht Prsd


Similarly we can also write a java program to remove the vowels from the string using a method.

import java.util.Scanner;
public class RemoveVowelsUsingMethod
{
   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


Also read – java program to remove consonants from a string


remove vowels from string java using for loop

In the below java program we are using for loop to remove vowels and ArrayList to maintain all vowels.

import java.util.ArrayList;
import java.util.List;
public class RemoveVowelsUsingForLoop
{
   public static void main(String[] args)
   {
      String str = "Hello world core java";
      String strResult = removeVowels(str);
      System.out.println("Remove vowels using for loop: " + strResult);
   }
   private static String removeVowels(String str)
   {
      List<Character> al = new ArrayList<Character>();
      al.add('a');
      al.add('e');
      al.add('i');
      al.add('o');
      al.add('u');
      al.add('A');
      al.add('E');
      al.add('I');
      al.add('O');
      al.add('U');
      StringBuffer sb = new StringBuffer(str.toLowerCase());
      for(int a = 0; a < sb.length(); a++)
      {
         if(al.contains(sb.charAt(a)))
         {
            sb.replace(a, a + 1, "");
            a--;
         }
      }
      return sb.toString();
   }
}


Output:

Remove vowels using for loop: hll wrld cr jv