java program to remove consonants from a string

Solutions on MaxInterview for java program to remove consonants from a string by the best coders in the world

showing results for - "java program to remove consonants from a string"
Imrane
11 Jan 2018
1// Java program to remove consonants from a string
2import java.util.Arrays;
3import java.util.List;
4public class RemoveConsonantsFromString
5{
6   public static void main(String[] args) 
7   {
8      String str = "hello world core java";
9      System.out.println("Remove consonants from a string: ");
10      System.out.println(removeConsonantsFunction(str));
11   }
12   static boolean checkAlphabet(char ch) 
13   { 
14      if(ch >= 'a' && ch <= 'z')
15         return true;
16      if(ch >= 'A' && ch <= 'Z') 
17         return true; 
18      return false; 
19   }
20   static String removeConsonantsFunction(String strConsonant)
21   {
22      Character[] chVowel = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
23      List<Character> li = Arrays.asList(chVowel);
24      StringBuffer sb = new StringBuffer(strConsonant);
25      for(int a = 0; a < sb.length(); a++)
26      {
27         if(checkAlphabet(sb.charAt(a)) && !li.contains(sb.charAt(a))) 
28         { 
29            sb.replace(a, a + 1, ""); 
30            a--; 
31         }
32      }
33      return sb.toString(); 
34   }
35}