要判断boy是不是后者中的一部分,不用循环,只要用String类的indexOf函数就行了。
代码如下:
public class HH {
public static void main(String[] args) {
String s="he is a boy";
int result=s.indexOf("boy");
if(result>=0){
System.out.println("boy是he is a boy的一部分");
}else{
System.out.println("boy不是he is a boy的一部分");
}
}
}
运行结果:
boy是he is a boy的一部分
java如何实现在一个字符串中查找另一个字符串
最新推荐文章于 2023-06-18 23:00:56 发布
本文介绍了一种简单的方法来判断一个字符串是否包含另一个字符串。通过使用Java中的indexOf函数,可以快速确定目标子字符串是否存在,无需循环遍历。
1025

被折叠的 条评论
为什么被折叠?



