indexOf 方法
返回 String 对象内第一次出现子字符串的字符位置。
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。
即indexOf()括号内所包含的字符在该字符串内的循序位置,在第几位就返回几-1
例子:
str1=asdfkju,str1.indexOf('d'),则返回的值是2。如果有重复的字符出现,以第一个字符为准。如果没有找到子字符串, 则返回 -1。
public class IndexOfTEST {
public static void main(String[] args) {
System.out.println("input a string:");
Scanner sc = new Scanner(System.in);
String a = sc.next();
String str1 = "BABEBIBOBUBABEBIBOBU";
int s = str1.indexOf(a);
System.out.println(s);
}
}