2. Checking for Vowel Using the indexOf method
2. 使用indexOf法检查元音。
1. Overview
1.概述
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
2.使用indexOf方法检查元音
As we know all the vowels, we could add them, in both upper and lowercase, to a String:
由于我们知道所有的元音,我们可以将它们以大写和小写的方式添加到String。
String VOWELS = "aeiouAEIOU";
We can use the indexOf method in the String class to see if the character is present:
我们可以使用indexOf方法在String类中查看该字符是否存在。
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:
如果该字符存在,那么索引将不会是-1。如果是-1,那么该字符就不在元音集里。让我们来测试一下。
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:
在这里,我们在Java中使用了一个char。如果我们的字符是一个单字符的String对象,我们可以使用一个不同的实现。
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.
我们可以看到,这个方法的实现开销很小。然而,我们必须遍历元音字符串中10个可能的元音,以确定某物是否在该组中。
3. Checking for Vowels Using switch
3.使用switch检查元音
We could, instead, use the switch statement where each vowel is a separate case:
相反,我们可以使用switch语句,其中每个元音是一个单独的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.
由于Java在switch语句中支持String,我们也可以用单字符的字符串实现。
4. Checking for Vowels using Regular Expressions
4.使用正则表达式检查元音
While we can implement our own string matching algorithms, the Java Regular Expressions engine allows us to powerfully match strings.
虽然我们可以实现自己的字符串匹配算法,但Java 常规表达式引擎使我们能够强有力地匹配字符串。
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:
让我们来实现我们对有单个字符的String对象的匹配算法。
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:
我们应该注意到,这要求输入是一个字符串,而不是一个字符。尽管我们可以在Character类的toString方法的帮助下将字符转换成String。
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?
5.我们应该使用哪种解决方案?
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.
基于字符串的解决方案可能是最简单易懂的,而且性能相当好,因为它只需要为每个字符的分类检查最多10个选项。
However, we’d generally expect a switch statement to perform faster than a String lookup.
然而,我们通常期望switch语句比String查询执行得更快。
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.
正则表达式解决方案应该表现得非常好,因为正则表达式在Pattern的编译方法中被优化。然而,正则表达式的实现可能比较复杂,对于检测元音这样简单的事情,可能不值得这么复杂。同样地,如果我们正在处理char值,那么正则表达式需要一些转换,而其他方法则不需要。
However, using regular expressions allows us to implement sophisticated expressions to classify characters.
然而,使用正则表达式使我们能够实现复杂的表达式来对字符进行分类。
6. Conclusion
6.结语
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.
在这篇文章中,我们看到了一些不同的方法来识别一个字符是否是元音。我们看到了如何使用一个包含所有元音的字符串,以及如何实现一个switch语句。
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.
一如既往,本教程的完整代码可以在GitHub上找到。