Java Coding Series: How to check vowel is present in the string?

sandeep negi
1 min readJun 2, 2021
  • Approach 1 : Most Useful, We will use regex to find wether vowels present in the stirng or not.
public static Boolean isVowelPresent(String sample) {        return sample.toLowerCase().matches(".*[aeiou].*");
}
  • Approach 2 : Traversing one by one and finding whether vowel is present or not using indexOf method
public static Boolean isVowelPresent(String sample) {
char vowelArr[] = { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < vowelArr.length; i++) {
char currentVowel = vowelArr[i];
if(sample.indexOf(currentVowel)!=-1){
return true;
}
}
return false;
}
}
  • Approach 3 : Traversing one by one and finding the vowel old fashion
public static Boolean isVowelPresent(String sample) {
char vowelArr[] = { 'a', 'e', 'i', 'o', 'u' };
for (int i = 0; i < vowelArr.length; i++) {
char currentVowel = vowelArr[i];
for (int j = 0; j < sample.length(); j++) {
if (currentVowel == sample.charAt(j)) {
return true;
}
}
}
return false;
}
}

--

--

sandeep negi

Software Engineer, who loves challenges and new technologies. :)