Four methods of String.indexOf()
String.indexOf(String str)
Official interpretation:
Returns the index of the first occurrence of the specified substring in this string. The returned index is the minimum value k, where: this.startsWith(str, k) returns - 1 if there is no such k value.
Parameters:
str – substring to search.
return:
Specifies the index of the first occurrence of the substring, or - 1 if there is no such occurrence.
public int indexOf(String str) { return indexOf(str, 0); }
public static void main(String[] args) { String str="abcdefg"; System.out.println("String.indexOf(String s):"+str.indexOf("b")); } }
String.indexOf(String str ,int from index)
Official interpretation:
Returns the index of the first occurrence of the specified substring in this string, starting from the specified index.
The returned index is the minimum value K, where: k > = fromindex & & this.startswith (STR, K)
If no such k value exists, - 1 is returned.
Parameters:
str – substring to search.
fromIndex – the index to start the search.
return:
Specifies the index of the first occurrence of the substring, starting from the specified index. If there is no such occurrence, it is - 1
public int indexOf(String str, int fromIndex) { return indexOf(value, 0, value.length, str.value, 0, str.value.length, fromIndex); }
public static void main(String[] args) { String str="abcdefgabcaaabc"; // System.out.println("String.indexOf(String s):"+str.indexOf("b")); System.out.println("String.indexOf(String str ,int fromIndex):"+str.indexOf("abc",1)); } }
String.indexOf(int ch)
Official interpretation:
Returns the index of the first occurrence of the specified character in this String. If the character of the value ch appears in the character sequence represented by this String object, the index of the first occurrence (expressed in Unicode code units) is returned. For CH values in the range of 0 to 0xFFFF (inclusive), this is the minimum value K satisfying the following conditions: this.charAt(k) == ch is true. For other values of CH, it is the minimum value K satisfying the following conditions: this.codePointAt(k) == ch is true. In either case, - 1 is returned if such a character does not appear in the String.
Parameters:
ch – one character (Unicode code point).
return:
The index of the first occurrence of the character in the character sequence represented by this object. If the character does not appear, it is - 1.
public int indexOf(int ch) { return indexOf(ch, 0); }
public static void main(String[] args) { String str="abcdefgabcaaabc"; // System.out.println("String.indexOf(String s):"+str.indexOf("b")); // System.out.println("String.indexOf(String str ,int fromIndex):"+str.indexOf("abc ",1)); System.out.println("String.indexOf(int ch):"+str.indexOf('b')); }
String.indexOf(int ch,int fromIndex)
Official interpretation:
Returns the index of the first occurrence of the specified character in this string, starting with the specified index.
If the character of the value ch appears in the character sequence represented by this String object and the index is not less than fromIndex, the index of the first occurrence of this class is returned. For the ch value in the range of 0 to 0xFFFF (inclusive), this is the minimum value K satisfying the following conditions: (this. Charat (k) = = CH) & & (k > = fromIndex) is true. For other values of CH, it is the minimum value K satisfying the following conditions: (this. Codepointat (k) = = CH) & & (k > = fromIndex) is true. In either case, - 1 is returned if no such character appears at or after the fromIndex position in this String.
There is no limit to the value of fromIndex. If it is negative, it has the same effect as if it were zero: you can search the entire string. If it is greater than the length of this string, the effect is the same as if it were equal to the length of this string: Return - 1.
All indexes are specified as char values (Unicode code units).
Parameters:
ch – one character (Unicode code point).
fromIndex – the index to start the search.
return:
The index of the first occurrence of the character in the character sequence represented by this object. The index is greater than or equal to fromIndex. If the character does not appear, it is - 1.
public int indexOf(int ch, int fromIndex) { final int max = value.length; if (fromIndex < 0) { fromIndex = 0; } else if (fromIndex >= max) { // Note: fromIndex might be near -1>>>1. return -1; }
public static void main(String[] args) { String str="abcdefgabcaaabc"; // System.out.println("String.indexOf(String s):"+str.indexOf("b")); // System.out.println("String.indexOf(String str ,int fromIndex):"+str.indexOf("abc ",1)); // System.out.println("String.indexOf(int ch):"+str.indexOf('b')); System.out.println("String.indexOf(int ch ,int fromIndex): "+str.indexOf('a', 3)); }