酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Eclipse 中 Java 同时查多个字符串的方法及示例代码

在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"。如果字符串包含要查找的字符串,则输出相应的消息;如果不包含,则输出另外的消息。 ######[AI写代码神器 | 947点数解答 | 2024-02-29 14:09:19]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]