比较有多个字符的字符 [英] Comparing a char with multiple characters

查看:141
本文介绍了比较有多个字符的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道的结果(||ē||我||Ø|| U)是布尔所以无法比较,但我们如何检查多个字符presence?

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?

推荐答案

这是不是做你想要什么。请使用堆栈开关语句:

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

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

或者,因为我是一个的正则表达式发烧友的,这里使用的常规EX pressions 办法! :)

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++;

<罢工>的还有就是在这样的code错误固定以后,<一个href=\"http://stackoverflow.com/questions/24977783/comparing-a-char-with-multiple-characters/24977865?noredirect=1#comment38828914_24977865\">thanks到user2980077

这篇关于比较有多个字符的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆