string 中 indexOf 和 substring
使用indexOf( )方法可以获取字符串中第一次出现字符的下标
public class main {
public static void main(String[] args) {
String str1 = "wuhu";
System.out.println(str1.indexOf('w')); // 0
System.out.println(str1.indexOf('H')); // -1 因为不存在于字符串中
String str2 = "qifei";
System.out.println(str2.indexOf('f')); // 2
}
}
使用substring可以截取指定位置的字符串
public class main {
public static void main(String[] args) {
String str1 = "wuhuqifei";
System.out.println(str1.substring(0,4)); //wuhu
System.out.println(str1.indexOf('H'));
}
}