Java Program to Delete Vowels from String
Write a Java Program to Remove or Delete Vowels from string using inbuilt function . For this program, you have to first ask to the user to enter the string and start deleting/removing all the vowels present in the string as shown in the following program.
Since, We have two methods to delete vowels from the string : –
First method that we already discussed in the previous post that is to Program to Remove or Delete vowels manually .
Second method is shortcut method that uses the method replaceAll( ) to replace all the vowels with no-space from the string then copy that string into another string without having any vowels.
Java String replaceAll() :
The java string replaceAll() method returns a string replacing all the sequence of characters matching regex and replacement string.
public String replaceAll(String regex, String replacement)
- regex — This is the regular expression to which this string is to be matched.
- replacement — This is the string to be substituted for each match.
This method returns the resulting string.
Following Java program removes all the vowels present in the string using inbuilt function.We use the function named replaceAll() to remove or delete all the vowels from the string.
Here the program is successfully compiled(build) and run (Netbeans) on the windows System and produce output below .Let’s look at the following program :
SOURCE CODE : :
public class Delete_Vowels
public static void main(String args)
String str1, str2;
Scanner scan = new Scanner(System.in);
System.out.print("Enter any String to delete vowels : ");
str1 = scan.nextLine();
System.out.println("\nBefore Removing Vowels, String is : \" "+ str1 +" \" ");
str2 = str1.replaceAll("[aeiouAEIOU]", "");
System.out.println("\nAfter Removing Vowels, String is : \" "+ str2 +" \" ");
OUTPUT : :
Enter any String to delete vowels : Hello CodezClub
Before Removing Vowels, String is : " Hello CodezClub "
After Removing Vowels, String is : " Hll CdzClb "