Java Program to Delete all Vowel Characters from String

  • Java program to remove all vowel alphabets from a string using replaceAll() method.
  • Java program to delete all vowels from a string without using any library function.

In this java program, we have to delete all occurrences of vowel characters from a given string and then print it on screen.

For Example,
Input String : TechCrashCourse
Output String : TchCrshCrs

Here, we are going to use two approaches of removing vowel alphabets from strings.


Java program to delete vowels from string using replaceAll method.

Here we are going to use replaceAll() method of String class. This method replaces any occurrence of any vowel character(lowercase or uppercase) with "". We are using "[AEIOUaeiou]" regular expression, which matches with any vowel alphabet.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to Delete Vowels from String
 */
public class DeleteVowels {
    public static void main(String args[]) {
        String str, output;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a String");
        str = scanner.nextLine();

        // Deleting Vowel alphabets from input
        // string using replaceAll method
        output = str.replaceAll("[AEIOUaeiou]", "");

        System.out.println("Output String\n" + output);
    }
}
Output
Enter a String
TechCrashCourse
Output String
TchCrshCrs
Enter a String
Apple
Output String
ppl

Java program to remove vowel characters from string without using library function.

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to Reverse a String using loop
 */
public class DeleteVowelsLoop {
    public static void main(String args[]) {
        String str, output = "";
        int length, i;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a String");
        str = scanner.nextLine();
        length = str.length();

        // Deleting vowel alphabets from input string
        for (i = 0; i < length; i++) {
            if ("AEIOUaeiou".indexOf(str.charAt(i)) == -1) {
                output = output + str.charAt(i);
            }
        }

        System.out.println("Output String : " + output);
    }
}
Output
Enter a String
Apple
Output String : ppl
Enter a String
TechCrashCourse
Output String : TchCrshCrs