gpt4 book ai didi

java - 一次搜索字符串中的多个字母

转载 作者:行者123 更新时间:2023-12-02 01:39:04 25 4
gpt4 key购买 nike

我需要在字符串中搜索四个字母单词末尾的元音。我可以做一个 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 类,并且我只能使用我们之前学到的东西。

最佳答案

String s = "FOUR"; // A sample string to look into
String vowels = "aeiouAEIOU"; // Vowels in both cases

if(vowels.indexOf(s.charAt(3)) >= 0){ // The last letter in a four-letter word is at index 4 - 1 = 3
System.out.println("Found!");
} else {
System.out.println("Not Found!");
}

关于java - 一次搜索字符串中的多个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20427417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com