write a java code to remove vowels from string code example
Example 1: Java program to remove consonants from a string
import java.util.Arrays;
import java.util.List;
public class RemoveConsonantsFromString
{
public static void main(String[] args)
{
String str = "hello world core java";
System.out.println("Remove consonants from a string: ");
System.out.println(removeConsonantsFunction(str));
}
static boolean checkAlphabet(char ch)
{
if(ch >= 'a' && ch <= 'z')
return true;
if(ch >= 'A' && ch <= 'Z')
return true;
return false;
}
static String removeConsonantsFunction(String strConsonant)
{
Character[] chVowel = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
List<Character> li = Arrays.asList(chVowel);
StringBuffer sb = new StringBuffer(strConsonant);
for(int a = 0; a < sb.length(); a++)
{
if(checkAlphabet(sb.charAt(a)) && !li.contains(sb.charAt(a)))
{
sb.replace(a, a + 1, "");
a--;
}
}
return sb.toString();
}
}
Example 2: Java program to delete vowels in a given string
import java.util.*;
public class RemoveVowelsInString
{
public static void main(String[] args)
{
String str = "Deekshit Prasad";
System.out.println("Given string: " + str);
str = str.replaceAll("[AaEeIiOoUu]", "");
System.out.println("After deleting vowels in given a string: " + str);
}
}