package study;
//查找字符串所要的字符并返回其索引(下标)
public class exercise {
public static void main(String[] args) {
String str="123456,123456";
String str1="23";
//从开头开始查找,没找到为-1
//找到该字符在字符串中第一次出现的索引(下标)
int a=str.indexOf('1');
int b=str.indexOf('2');
System.out.println(a);
System.out.println(b);
System.out.println("**********分割线*********");
//指定某位置后再开始查找该字符在字符串中第一次出现的索引(下标)
int c=str.indexOf('1', 4);
int d=str.indexOf('2', 5);
System.out.println(c);
System.out.println(d);
System.out.println("**********分割线*********");
//该指定子字符串在原字符串第一次出现的索引(下标)
int e=str.indexOf(str1);
System.out.println(e);
System.out.println("**********分割线*********");
//指定某位置后再开始查找该指定子字符串在原字符串第一次出现的索引(下标)
int f=str.indexOf(str1, 4);
System.out.println(f);
System.out.println("**********分割线*********");
}
}
package study;
//查找字符串所要的字符并返回其索引(下标)
public class exercises {
public static void main(String[] args) {
String str="123456,123456";
String str1="23";
//从末尾开始查找,没找到为-1
//找到该字符在字符串中最后一次出现的索引(下标)
int a=str.lastIndexOf('2');
System.out.println(a);
System.out.println("*********分割线*********");
//指定某位置后再开始查找该字符在字符串中最后一次出现的索引(下标)
int b=str.lastIndexOf('3', 11);//(要寻找的字符,限定从最后的第几个开始寻找)
System.out.println(b);
System.out.println("*********分割线*********");
//该指定子字符串在原字符串最后一次出现的索引(下标)
int c=str.lastIndexOf(str1);
System.out.println(c);
System.out.println("*********分割线*********");
//指定某位置后再开始查找该指定子字符串在原字符串最后一次出现的索引(下标)
int d=str.lastIndexOf(str1, 5);
System.out.println(d);
}
}