Home > Back-end >  Students learn in school record pen, thanks to the author
Students learn in school record pen, thanks to the author

Time:09-20

Explanation in Java indexOf () method

Introduce indexOf () two kinds of usage, and realize functions:

1, indexOf (String STR) : returns the specified character STR in the String method (caller) first appeared in the starting index, if no such characters in this String, it returns 1,
2, indexOf (String STR, int index) : returns the character from the index position began to find the STR in the String for the first time appear in the starting index, if no such characters in this String, it returns 1,

For example:

String STR="01234567890123456789"; System. The out. Println (STR. IndexOf (" 123 ")); System. The out. Println (STR. IndexOf (" 123 ", 5));

1

2

3

The results of the above procedure for 1 and 11, the first output function of indexOf () method is only one parameter, therefore return 123 first appeared in the index in STR, 1; 1 output function of indexOf () method has two parameters, the first parameter to find the string of 123, the second parameter is starting from 5 so location search, it can only be found in the STR second 123, returns to its starting index, to 11,



  • Related