In this tutorial we will see how to remove vowels from String in java.
Table of Contents [hide]
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
All Vowels Removed Successfully..!!
New String is : Jv2Blg
Explaination
- Take input String from Scanner str1
- Call
replaceAll()
method on String to replace[aeiouAEIOU]
with empty String""
- 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
String without vowels is : Jv2Blg
- Create HashSet named
vowelsSet
and add all vowels to it. - Iterate over String
str
and check ifvowelsSet
does not contain the character. - If
vowelsSet
does not contain the character, then add it to result StringresultStr
- Once loop is complete, then print the
resultStr
.
That’s all about Java program to remove vowels from String.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.