Remove vowels from a string in java using for loop

Let’s learn to remove vowels from a string in java using for loop.

Remove vowels from a string in java using for loop

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

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