获取子字符串索引位置--indexOf方法与lastIndexOf方法
package com;
public class GetStringIndex {
public static void main(String[] args) {
String str ="明月几时有,把酒问青天";
System.out.println(str);
int charIndex =str.indexOf("酒");
System.out.println("字符‘酒’首次出现的索引为:"+charIndex);
}
}
-——————————————————————————————————————————-
package com;
public class GetStringIndex {
public static void main(String[] args) {
String str ="we are the world";
System.out.println(str);
int charIndex =str.indexOf("e");
System.out.println("字符‘e’首次出现的索引为:"+charIndex);
int charIndex2 =str.indexOf("e",4);
System.out.println("字符‘e’在索引4之后第一次出现的位置:"+charIndex2);
System.out.println();
String str2 = "let it go!let it go!";
System.out.println(str2);
int index = str2.lastIndexOf("t");
System.out.println("字符t最后一次出现的位置:"+index);
int index2 =str2.lastIndexOf("t",14);
System.out.println("从索引14的位置往前查第一个t出现的位置:"+index2);
}
}
还没有评论,来说两句吧...