public class StringDemo3 {
public static void main(String[] args) {
String str="xingkong jiuban changzai";
// int length():获取字符串的长度。
System.out.println("←--length--获取字符串的长度--→");
System.out.println("length:"+str.length());
// charAt(int index):获取指定索引位置的字符
System.out.println("←--charAt--获取指定索引位置的字符--→");
System.out.println(str.charAt(5));
//-1 代表没找到
//indexOf 从左向右
// 返回指定字符在此字符串中第一次出现处的索引。
System.out.println("←--indexOf--返回指定字符在此字符串中第一次出现处的索引--→");
System.out.println(str.indexOf('g'));
System.out.println(str.indexOf("ng"));
// 从指定位置开始到指定位置结束截取字符串。
System.out.println("←--indexOf--从指定位置开始到指定位置结束截取字符串。--→");
System.out.println(str.indexOf('g',7));
System.out.println(str.indexOf("ng",7));
//lastindexOf 从右向左
// lastIndexOf(Object o)返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 1。
System.out.println("←--lastIndexOf--返回此列表中最后出现的指定元素的索引--→");
System.out.println(str.lastIndexOf('g'));
System.out.println(str.lastIndexOf("ng"));
// 索引不能被改变,从右向左找,只是按照索引数往回找,比如7,就是顺数的第7个往回找常数
// 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
System.out.println("←--lastIndexOf--返回此列表中最后出现的指定元素的索引--→");
System.out.println(str.lastIndexOf('g',7));
System.out.println(str.lastIndexOf("ng",7));
// substring(int start) 从指定位置开始截取字符串,默认到末尾。
// 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
System.out.println("←--substring--从指定位置开始截取字符串,默认到末尾。--→");
String str1=str.substring(5);
System.out.println(str1);
// 从指定位置开始到指定位置结束截取字符串。
// [startindex,endindex)左闭右开格式,这里的索引是从0开始的,截断到9结束,实际上是获取第6个到10个的5个索引,中间包含一个空格
System.out.println("←--substring--从指定位置开始到指定位置结束截取字符串--→");
String str2=str.substring(5, 10);
System.out.println(str2);
}
}