Search code examples
javastringletters

Alphabetical value


Return the number of times that the string "code" appears anywhere in the given string,
except we'll accept any letter for the 'd', so "cope" and "cooe" count.

Is there a value that I can input that means any letter or do I have to make a case for every letter of the alphabet?


Solution

  • Use a regex:

    string.matches("co[a-z]e")
    

    To count the number of matches:

    int count = 0;
    Matcher m = Pattern.compile("co[a-z]e").matcher(string);
    while(m.find()) {
        count++;
    }