contains和indexof都可以作为判断是否包含的方法,那么它们在使用上肯定有些区别,我们通过代码开看看这些:
public class con {
public static void main(String[] args) {
String a="abcdefghijklmn";
String b="defghi";
String c="rst";
System.out.println(a.contains(b));//如果包含,contains返回true
System.out.println(a.indexOf(b));//如果包含,indexOf返回字符串匹配的首索引位置,3
System.out.println(a.contains(c));//如果不包含,contains返回false
System.out.println(a.indexOf(c));//如果不包含,indexOf返回-1
}
}