酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

eclipse 如何同时查多个字符串

在Java中,可以使用`String.contains()`方法来判断一个字符串是否包含另一个字符串。如果要同时查找多个字符串,你可以使用一个循环来遍历每个要查找的字符串,然后在每个循环中使用`contains()`方法来判断字符串是否存在。 下面是一个示例代码: ```java public class Main { public static void main(String[] args) { String str = "Hello, world!"; String[] searchStrings = {"Hello", "world", "foo"}; for (String searchString : searchStrings) { if (str.contains(searchString)) { System.out.println("String '" + searchString + "' is found in '" + str + "'."); } else { System.out.println("String '" + searchString + "' is not found in '" + str + "'."); } } } } ``` 输出结果: ``` String 'Hello' is found in 'Hello, world!'. String 'world' is found in 'Hello, world!'. String 'foo' is not found in 'Hello, world!'. ``` 在上面的示例中,我们在一个循环中查找了三个字符串,即"Hello"、"world"和"foo"。首先,我们检查字符串是否包含"Hello",然后检查是否包含"world",最后检查是否包含"foo"。如果字符串包含要查找的字符串,则输出相应的消息;如果不包含,则输出另外的消息。 [2024-02-29 14:09:19 | AI写代码神器 | 947点数解答]

相关提问