java一次在字符串中搜索多个字母
我需要在字符串中搜索四个字母单词末尾的元音。我可以做一个if-else树,逐个搜索每个字母,但我想简化一下
您通常通过以下方式搜索信件:
String s = four
if (s.indexOf ('i') = 4)
System.out.println("Found");
else
System.out.println("Not found");
我是否可以用以下内容替换indexOf
的参数:
s.indexOf ('a','e','i','o','u')
这会让一切变得容易得多
不幸的是,我不能使用Regexp类,而且我只需要使用我们之前学到的东西
# 1 楼答案
# 2 楼答案
正则表达式?我相信这是有效的。“后跟e i或u的任意3个单词字符。”
好吧,如果你不能使用正则表达式,那么为什么不使用像EDIT:Modified这样的东西来与GaborSch的答案保持一致——我的替代算法非常接近,但是使用char而不是创建另一个字符串要好得多!向加博什投一票
# 3 楼答案
这是^{} 的作业,也是一个合适的正则表达式:
如果不允许使用正则表达式,可以为此定义函数: