How does Java return the specified string subscript
This article mainly explains "how Java returns the specified string subscript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Java returns the specified string subscript"!
Use the indexOf or lastIndexOf method in Java to obtain the subscript for the first occurrence of a specified character in a string.
1.indexOf
Public int indexOf (String str, int fromIndex) looks for the first occurrence of the subscript of String str in a string. Starting with int fromIndex, the return value is an integer.
String str = "wanag"; str.indexOf ("a"); / / returns 1
2.lastIndexOf
Public int lastIndexOf (String str, int fromIndex) reverse lookup.
String str = "wanag"; str.lastIndexOf ("a"); / / return 3 so far. I believe you have a better understanding of "how Java returns the specified string subscript". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!