The indexOf() method is utilized to find the index of the first appearance of the character in the string and the character is present in the method as argument.
Method Definition: int indexOf(int ch)
Return Type: It returns the index of the character stated in the argument.
Example #1:
// Scala program of int indexOf() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Applying indexOf method val result = "Nidhi".indexOf('i') // Displays output println(result) } } |
chevron_right
filter_none
Output:
1
So, here the first occurrence of ‘i’ is at index one So, 1 is returned as output.
Example #2:
// Scala program of int indexOf() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Applying indexOf method val result = "Nidhi".indexOf('h') // Displays output println(result) } } |
chevron_right
filter_none
Output:
3
Recommended Posts:
- Scala String indexOf(String str) method with example
- Scala String indexOf(String str, int fromIndex) method with example
- Scala String indexOf(int ch, int fromIndex) method with example
- Scala Iterator indexOf() method with example
- Scala List indexOf() method with example
- Scala String startsWith(String prefix, int toffset) method with example
- Scala String lastIndexOf(String str, int fromIndex) method with example
- Scala String regionMatches(int toffset, String other, int offset, int len) method with example
- Scala String lastIndexOf(String str) method with example
- Scala String startsWith(String prefix) method with example
- Scala String split(String regex, int limit) method with example
- Scala String contentEquals() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala String subSequence() method with example
- Scala String equalsIgnoreCase() method with example
- Scala String endsWith() method with example
- Scala String compareToIgnoreCase() method with example
- Scala String charAt() method with example
- Scala String equals() method with example
- Scala String compareTo() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

