Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

2. Checking for Vowel Using the indexOf method

1. Overview

When processing characters from a String, we may wish to classify them according to whether they're in a particular group. For example, characters in the English alphabet are either vowels or consonants.

In this tutorial, we'll look at a few methods to check if a character is a vowel. We could easily extend these methods to other groups of characters.

2. Checking for Vowel Using the indexOf Method

As we know all the vowels, we could add them, in both upper and lowercase, to a String:

String VOWELS = "aeiouAEIOU";

We can use the indexOf method in the String class to see if the character is present:

boolean isInVowelsString(char c) {
    return VOWELS.indexOf(c) != -1;
}

If the character is present, the index will not be -1. If it's -1, then the character is not in the set of vowels. Let's test this:

assertThat(isInVowelsString('e')).isTrue();
assertThat(isInVowelsString('z')).isFalse();

Here, we're using a char in Java. If our character was a single character String object, we could use a different implementation:

boolean isInVowelsString(String c) {
    return VOWELS.contains(c);
}

It would pass the same tests:

assertThat(isInVowelsString("e")).isTrue();
assertThat(isInVowelsString("z")).isFalse();

As we can see, there's a small implementation overhead for this method. However, we have to iterate through 10 possible vowels in the vowels string to determine if something is in the group or not.

3. Checking for Vowels Using switch

We could, instead, use the switch statement where each vowel is a separate case:

boolean isVowelBySwitch(char c) {
    switch (c) {
        case 'a':            
        case 'e':           
        case 'i':           
        case 'o':            
        case 'u':            
        case 'A':
        case 'E':            
        case 'I':           
        case 'O':            
        case 'U':
            return true;
        default:
            return false;
    }
}

We can also test this:

assertThat(isVowelBySwitch('e')).isTrue();
assertThat(isVowelBySwitch('z')).isFalse();

Since Java supports String in switch statements, we could also implement this with single-character strings.

4. Checking for Vowels using Regular Expressions

While we can implement our own string matching algorithms, the Java Regular Expressions engine allows us to powerfully match strings.

Let's build a regular expression to recognize a vowel:

Pattern VOWELS_PATTERN = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);

The [] are used to represent a character class. We've put the vowels into this class in lowercase only, as we can match them in a case-insensitive way.

Let's implement our matching algorithm for String objects with a single character in:

boolean isVowelByRegex(String c) {
    return VOWELS_PATTERN.matcher(c).matches();
}

Let's test this:

assertThat(isVowelByRegex("e")).isTrue();
assertThat(isVowelByRegex("E")).isTrue();

As we can see, the regular expression is case insensitive.

We should note that this requires the input to be a String, not a character. Though we can convert a character to String with the help of the Character class's toString method:

assertThat(isVowelByRegex(Character.toString('e'))).isTrue();

Using regular expressions makes it straightforward to handle the general case of this problem. We can specify any grouping of characters using character classes, including character ranges.

5. Which Solution Should We Use?

The String-based solution is probably the simplest to understand and performs pretty well since it only needs to check a maximum of 10 options for every character it classifies.

However, we'd generally expect a switch statement to perform faster than a String lookup.

The regular expressions solution should perform very well, as regular expressions are optimized during the compile method of Pattern. However, regular expressions can be more complicated to implement and may not be worth the complexity for something as simple as detecting vowels. Similarly, if we are working with char values, then the regular expression requires some conversion the other methods do not.

However, using regular expressions allows us to implement sophisticated expressions to classify characters.

6. Conclusion

In this article, we have seen a few different ways to identify whether a character is a vowel. We saw how a string with all the vowels could be used and how to implement a switch statement.

Finally, we saw how regular expressions could be used to solve this and more general cases.

As always, the complete code for this tutorial can be found over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!