Will return number of vowels in given string 'str' - Java java.lang

Java examples for java.lang:String Search

Description

Will return number of vowels in given string 'str'

Demo Code


//package com.java2s;

public class Main {
    /** String containing all ascii-vowels */
    public static final String VOWELS = "aAeEiIuUoOyY";

    /** Will return number of vowels in given string 'str'
     * @param str string where you want to count vowels
     * @return number of vowels in string 'str' */
    public static int getNumberOfVowels(String str) {
        return getNumberOfXXX(str, VOWELS);
    }/*from  w ww.j  a  va 2  s . co  m*/

    /** Used only internally by methods numberOfVowels and numberOfConsonants,
     * for purpose of avoiding redundant code */
    private static int getNumberOfXXX(String str, String charGroup) {
        int num = 0;
        for (char letter : str.toCharArray()) {
            if (charGroup.contains(Character.toString(letter)))
                num += 1;
        }
        return num;
    }
}

Related Tutorials