TEbogo :
I am writing a method to replace all vowels in a string with a given character, but it does not work for strings with more than one vowel. It works for "heel" but not for "hello". Please help. My code below:
public Boolean isVowel(char ch){
char ch2 = Character.toLowerCase(ch);
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for(int i = 0; i < vowels.length; i++){
if (ch2 == vowels[i]) {
return true;
}
}
return false;
}
public String replaceVowels(String phrase, char ch){
String newP = "";
for(int i = 0; i < phrase.length(); i++){
char c = phrase.charAt(i);
Boolean vowel = isVowel(c);
if(vowel){
newP = phrase.replace(c, ch);
}
}
return newP;
}
Alex :
public String replaceVowels(final String phrase,final String ch) {
return phrase.replaceAll("[aeiou]", ch);
}
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=403365&siteId=1