Can you give me information about indexOf () method please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you give me information about indexOf () method please?

String txt = "Please locate where 'locate' occurs!"; System.out.println(txt.indexOf("locate")); it outputs 7. How this happens

9th May 2020, 4:53 PM
Umidbek
Umidbek - avatar
2 Answers
+ 1
The indexOf() returns the index of first occurrence of "locate" which begins at index 7. Use this to return the last occurrence- System.out.print(txt.lastIndexOf("locate"));
9th May 2020, 5:03 PM
Avinesh
Avinesh - avatar
0
Ok, thanks
9th May 2020, 5:23 PM
Umidbek
Umidbek - avatar