Java program to remove vowels from String

In this tutorial we will see how to remove vowels from String in java.

Using replaceAll() method

We will use String’s replaceAll() method to remove vowels from String.

Output:

Enter a String : Java2Blog
All Vowels Removed Successfully..!!
New String is : Jv2Blg

Explaination

  1. Take input String from Scanner str1
  2. Call replaceAll() method on String to replace [aeiouAEIOU] with empty String ""
  3. Print the result String str2.

Using iterative method

Here is the program to remove vowels from String without replace() or replaceAll() method.

Output:

Original String is : Java2Blog
String without vowels is : Jv2Blg
  1. Create HashSet named vowelsSet and add all vowels to it.
  2. Iterate over String str and check if vowelsSet does not contain the character.
  3. If vowelsSet does not contain the character, then add it to result String resultStr
  4. Once loop is complete, then print the resultStr.

That’s all about Java program to remove vowels from String.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *