Configuration

Night mode

IndexOf

Introduction To Java Course

Engineers From These Top Companies and Universities Trust EXLskills

1M+ Professionals | 100+ Institutions

This is the EXLskills free and open-source Introduction To Java Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.

Is this course FREE?

Yes, this a 100% free course that you can contribute to on GitHub here!

Have more questions?

String Methods

IndexOf

Let's discuss what the concept of "index" means in Java. Index numbering starts with zero and totals up characters. The method indexOf() returns the index of the first occurrence of a String or char in a targeted String. Take a look below to see how this method is used.

Indexing starts with zero, which is why the program above will print 6, not 7. Therefore, if you had a String "Java":

Index: 0 1 2 3

String: J a v a

Another way to use the indexOf() method is by putting an valid index after the target String. This tells the method where you want to start searching for the target String. As we see in the example above, the index of "World" is 6, meaning if we start searching from index 7, we will not find the String "World", thus returning -1.