package 字符串;
// indexOf 如果能查找到。返回索引值,如果查找不到,返回-1;从左往右进行查询
public class 字符串的查找 {
public static void main(String[] args) {
String str = "onetwothreefour";
// int a = str.indexOf("e");
// int b = str.indexOf("e", 4);
// int c = str.indexOf("e", 12);
// int a = str.lastIndexOf("o");
// int b = str.lastIndexOf("o", 4);
// int c = str.lastIndexOf("o", 12);
char a = str.charAt(2);
char b = str.charAt(5);
char c = str.charAt(13);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
// lastIndexOf()方法的查找策略是从右往左查找,如果不指定起始索引,则从字符串的末尾开始查找
// String 类中提供了charAt()方法,用于返回指定字符串中指定索引的字符,该方法的语法形式如下 str.charAt(int index);
注:在这边主要介绍了三种java中字符串的查找用这几个简单的函数,可以完成复杂的查找过程,具体解释都在代码段的注释里了,一键三连啊亲。