Java中字符串中子串的索引位置查找的方法:
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引位置。
2、int indexOf(String str, int startIndex):从指定的索引位置开始,返回第一次出现指定子字符串在此字符串中的索引位置。
3、int lastIndexOf(String str) :返回此字符串中最后一次出现指定子字符串的索引位置。
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引位置。
2、int indexOf(String str, int startIndex):从指定的索引位置开始,返回第一次出现指定子字符串在此字符串中的索引位置。
3、int lastIndexOf(String str) :返回此字符串中最后一次出现指定子字符串的索引位置。
4、int lastIndexOf(String str, int endIndex) :返回从索引位置0开始到指定位置,最后一次出现子串的索引位置。
import java.math.*; import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub String S = "0123456789 0123456789 0123456789"; System.out.println(S.indexOf("23"));//输出2 (代表第一次出现字符串“23”的下标位置为2) System.out.println(S.indexOf("23", 4));//输出13 (代表从下标位置4开始,第一次出现字符串“23”的下标位置为13) System.out.println(S.lastIndexOf("89"));//输出30 (代表最后一次出现字符串“89”的下标位置为30) System.out.println(S.lastIndexOf("23",11));//输出2 (代表从下标的位置0开始到下标的位置11结束最后一次出现字符串“23”的下标位置为2) } }