Java 将一个字符与多个字符进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24977783/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:08:12  来源:igfitidea点击:

Comparing a char with multiple characters

javaarraysif-statementcharboolean

提问by torrtuga

for(int j=0 ; j<str.length() ; j++) {
    if(char[j]==(a||e||i||o||u))
        count++;
}

I know the result of (a||e||i||o||u)is a Boolean so can't compare but how can we check for multiple character presence?

我知道结果(a||e||i||o||u)是一个布尔值所以无法比较但我们如何检查多个字符的存在?

采纳答案by Unihedron

This is not doing what you want. Please use a stack switchstatement:

这不是在做你想做的。请使用堆栈switch语句:

for(int j = 0; j < str.length(); j++)
     switch(str.charAt(j)) {
         case 'a':
         case 'e':
         case 'i':
         case 'o':
         case 'u':
             count++;
     }

Or, since I'm a regex enthusiast, here's an approach using regular expressions! :)

或者,因为我是一个正则表达式爱好者,这里有一个使用正则表达式的方法!:)

Matcher matcher = Pattern.compile("[aeiou]").matcher(str);
while(matcher.find())
    count++;

There was a mistake in this code fixed later on, thanks to user2980077

稍后修复此代码中的错误,感谢 user2980077

回答by Arian Kiehr

If you use the classes you can try with regex or simple String

如果您使用这些类,您可以尝试使用正则表达式或简单字符串

String s = "aeiouaeiou";//string to count
int count = 0;
for (int i = 0; i < s.length(); i++) {

  //One method
  if ("aeiou".indexOf( s.charAt(i) ) >= 0) {
    count++;
  }

  //Another one
  if (Character.toString( s.charAt(i) ).matches("[aeiou]")) {
    count++;
  }

}

回答by ajb

One more for the clever regex department:

另一个聪明的正则表达式部门:

count = str.replaceAll("[^aeiou]","").length();

回答by GrgaMrga

If I reallyneed to work with a char[]array and not with a Stringinstance, I always use the Characterclass and regular expressions. If you don't know what regular expressions are you should learn them because they are very useful when working with strings. Also, you can practice at Regexr.

如果我真的需要使用char[]数组而不是String实例,我总是使用Character类和正则表达式。如果您不知道正则表达式是什么,您应该学习它们,因为它们在处理字符串时非常有用。此外,您可以在Regexr练习。

For your example I'd use this:

对于你的例子,我会用这个:

char[] data = "programming in Java is fun".toCharArray();
int counter = 0;

for(int i = 0; i<data.length; i++){
    if(Character.toString(data[i]).matches("[aeiou]")){
        counter++;
    }
}

System.out.println(counter); // writes: 8

What the if statement does is basically it makes a new Stringinstance containing the current character just to be able to use the methods from the Stringclass. The method boolean matches(String regex)checks whether your string satisfies the conditions given with the regexargument.

if 语句所做的基本上是创建一个String包含当前字符的新实例,以便能够使用String类中的方法。该方法boolean matches(String regex)检查您的字符串是否满足regex参数给出的条件。

回答by varnull

One more for Java 8:

Java 8 的另一个:

count = str.chars()
           .filter(c -> c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
           .count();

回答by Naman

One of the ways to do this in using a Listof Characterand in java9could be :

其中,在利用这样做的途径ListCharacter,并在java9可能是:

List<Character> vowels = List.of('a','e','i','o','u'); // pverloaded convenience factory method 
for(int j=0 ; j < str.length() ; j++) {
    if(vowels.contains(char[j])) {
        count++;
    }
}