Java 如何检查字符是否为元音?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19160921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if a char is a vowel?
提问by Kylo
This Java code is giving me trouble:
这段 Java 代码给我带来了麻烦:
String word = <Uses an input>
int y = 3;
char z;
do {
z = word.charAt(y);
if (z!='a' || z!='e' || z!='i' || z!='o' || z!='u')) {
for (int i = 0; i==y; i++) {
wordT = wordT + word.charAt(i);
} break;
}
} while(true);
I want to check if the third letter of word is a non-vowel, and if it is I want it to return the non-vowel and any characters preceding it. If it is a vowel, it checks the next letter in the string, if it's also a vowel then it checks the next one until it finds a non-vowel.
我想检查单词的第三个字母是否是非元音,如果是,我希望它返回非元音及其前面的任何字符。如果是元音,则检查字符串中的下一个字母,如果它也是元音,则检查下一个,直到找到非元音为止。
Example:
例子:
word = Jaemeas then wordT must = Jaem
word = Jaemeas 那么 wordT must = Jaem
Example 2:
示例 2:
word=Jaeoimus then wordT must =Jaeoim
word=Jaeoimus 那么 wordT 必须 =Jaeoim
The problem is with my if
statement, I can't figure out how to make it check all the vowels in that one line.
问题在于我的if
语句,我不知道如何让它检查那一行中的所有元音。
采纳答案by arshajii
Your condition is flawed. Think about the simpler version
你的条件有问题。考虑更简单的版本
z != 'a' || z != 'e'
If z
is 'a'
then the second half will be true since z
is not 'e'
(i.e. the whole condition is true), and if z
is 'e'
then the first half will be true since z
is not 'a'
(again, whole condition true). Of course, if z
is neither 'a'
nor 'e'
then both parts will be true. In other words, your condition will never be false!
如果z
是,'a'
则后半部分将为真,因为z
不是'e'
(即整个条件为真),如果z
是,'e'
则前半部分将为真,因为z
不是'a'
(同样,整个条件为真)。当然,如果z
两者都不'a'
是,'e'
那么这两个部分都是真的。换句话说,你的条件永远不会是假的!
You likely want &&
s there instead:
你可能想要&&
s 代替:
z != 'a' && z != 'e' && ...
Or perhaps:
也许:
"aeiou".indexOf(z) < 0
回答by Drifter64
For starters, you are checking if the letter is "not a" OR "not e" OR "not i" etc.
首先,您要检查字母是“not a”还是“not e”或“not i”等。
Lets say that the letter is i. Then the letter is not a, so that returns "True". Then the entire statement is True because i != a. I think what you are looking for is to AND the statements together, not OR them.
假设字母是 i。那么字母不是 a,因此返回“True”。那么整个语句为真,因为 i != a。我认为您正在寻找的是将这些语句 AND 在一起,而不是 OR 它们。
Once you do this, you need to look at how to increment y and check this again. If the first time you get a vowel, you want to see if the next character is a vowel too, or not. This only checks the character at location y=3.
完成此操作后,您需要查看如何增加 y 并再次检查。如果你第一次得到元音,你想看看下一个字符是否也是元音。这仅检查位置 y=3 处的字符。
回答by Silviu Burcea
Clean method to check for vowels:
检查元音的清洁方法:
public static boolean isVowel(char c) {
return "AEIOUaeiou".indexOf(c) != -1;
}
回答by Mehmet Sedat Güng?r
Actually there are much more efficient ways to check it but since you've asked what is the problem with yours, I can tell that the problem is you have to change those OR operators with AND operators. With your if statement, it will always be true.
实际上有更有效的方法来检查它,但既然你已经问过你的问题是什么,我可以说问题是你必须用 AND 运算符更改那些 OR 运算符。使用您的 if 语句,它将始终为真。
回答by user2838970
String word="Jaemeas";
String wordT="";
int y=3;
char z;
do{
z=word.charAt(y);
if(z!='a'&&z!='e'&&z!='i'&&z!='o'&&z!='u'&&y<word.length()){
for(int i = 0; i<=y;i++){
wordT=wordT+word.charAt(i);
}
break;
}
else{
y++;
}
}while(true);
here is my answer.
这是我的答案。
回答by jmtrachy
How about an approach using regular expressions? If you use the proper pattern you can get the results from the Matcher object using groups. In the code sample below the call to m.group(1) should return you the string you're looking for as long as there's a pattern match.
使用正则表达式的方法怎么样?如果您使用正确的模式,您可以使用组从 Matcher 对象中获得结果。在下面的代码示例中,只要存在模式匹配,对 m.group(1) 的调用就会返回您正在查找的字符串。
String wordT = null;
Pattern patternOne = Pattern.compile("^([\w]{2}[AEIOUaeiou]*[^AEIOUaeiou]{1}).*");
Matcher m = patternOne.matcher("Jaemeas");
if (m.matches()) {
wordT = m.group(1);
}
Just a little different approach that accomplishes the same goal.
只是实现相同目标的稍微不同的方法。
回答by paranza
I have declared a char[] constant for the VOWELS, then implemented a method that checks whether a char is a vowel or not (returning a boolean value). In my main method, I am declaring a string and converting it to an array of chars, so that I can pass the index of the char array as the parameter of my isVowel method:
我已经为 VOWELS 声明了一个 char[] 常量,然后实现了一个方法来检查一个 char 是否是元音(返回一个布尔值)。在我的 main 方法中,我声明了一个字符串并将其转换为一个字符数组,以便我可以将字符数组的索引作为我的 isVowel 方法的参数传递:
public class FindVowelsInString {
static final char[] VOWELS = {'a', 'e', 'i', 'o', 'u'};
public static void main(String[] args) {
String str = "hello";
char[] array = str.toCharArray();
//Check with a consonant
boolean vowelChecker = FindVowelsInString.isVowel(array[0]);
System.out.println("Is this a character a vowel?" + vowelChecker);
//Check with a vowel
boolean vowelChecker2 = FindVowelsInString.isVowel(array[1]);
System.out.println("Is this a character a vowel?" + vowelChecker2);
}
private static boolean isVowel(char vowel) {
boolean isVowel = false;
for (int i = 0; i < FindVowelsInString.getVowel().length; i++) {
if (FindVowelsInString.getVowel()[i] == vowel) {
isVowel = true;
}
}
return isVowel;
}
public static char[] getVowel() {
return FindVowelsInString.VOWELS;
}
}
回答by Jeremy Trifilo
So in event anyone ever comes across this and wants a easy compare method that can be used in many scenarios.
因此,万一有人遇到过这种情况,并且想要一种可以在许多情况下使用的简单比较方法。
Doesn't matter if it is UPPERCASE or lowercase. A-Z and a-z.
无论是大写还是小写都没有关系。AZ 和 az。
bool vowel = ((1 << letter) & 2130466) != 0;
布尔元音 = ((1 << 字母) & 2130466) != 0;
This is the easiest way I could think of. I tested this in C++ and on a 64bit PC so results may differ but basically there's only 32 bits available in a "32 bit integer" as such bit 64 and bit 32 get removed and you are left with a value from 1 - 26 when performing the "<< letter".
这是我能想到的最简单的方法。我在 C++ 和 64 位 PC 上对此进行了测试,因此结果可能会有所不同,但基本上“32 位整数”中只有 32 位可用,因为第 64 位和第 32 位被删除,并且您在执行时剩下 1 - 26 的值“<< 字母”。
If you don't understand how bits work sorry i'm not going go super in depth but the technique of
如果你不明白比特是如何工作的,对不起,我不会深入研究,但技术
1 << N is the same thing as 2^N power or creating a power of two.
1 << N 与 2^N 幂或创建 2 的幂相同。
So when we do 1 << N & X we checking if X contains the power of two that creates our vowel is located in this value 2130466. If the result doesn't equal 0 then it was successfully a vowel.
因此,当我们执行 1 << N & X 时,我们会检查 X 是否包含创建元音的 2 的幂,该值位于此值 2130466 中。如果结果不等于 0,则它是成功的元音。
This situation can apply to anything you use bits for and even values larger then 32 for an index will work in this case so long as the range of values is 0 to 31. So like the letters as mentioned before might be 65-90 or 97-122 but since but we keep remove 32 until we are left with a remainder ranging from 1-26. The remainder isn't how it actually works, but it gives you an idea of the process.
这种情况适用于您使用位的任何内容,甚至大于 32 的索引值也适用于这种情况,只要值的范围是 0 到 31。所以像前面提到的字母可能是 65-90 或 97 -122 但因为我们一直删除 32,直到剩下 1-26 的余数。其余的不是它实际的工作方式,但它可以让您了解该过程。
Something to keep in mind if you have no guarantee on the incoming letters it to check if the letter is below 'A' or above 'u'. As the results will always be false anyways.
如果您不能保证收到的信件,请记住要检查该信件是否低于“A”或高于“u”。因为无论如何结果总是错误的。
For example teh following will return a false vowel positive. "!" exclamation point is value 33 and it will provide the same bit value as 'A' or 'a' would.
例如,以下将返回假元音肯定。“!” 感叹号是值 33,它将提供与 'A' 或 'a' 相同的位值。