完整代码如下(关键代码加注释):
public class Test {
// 参数是需要判断的子字符串
public void judge(String subString) {
String string = "judge is subString";
// 返回指定子字符串在此字符串中第一次出现处的索引。如果它不作为一个子字符串出现,则返回 -1。
if (string.indexOf(subString) == -1) {
System.out.println("此字符串中不含" + subString + "这个子字符串");
} else {
System.out.println("此字符串中含有" + subString + "这个子字符串");
}
}
// 主方法
public static void main(String[] args) {
Test test = new Test();
test.judge("sub");
}
}
允许结果:
