Languages
[Edit]
EN

Java - String indexOf() method example

5 points
Created by:
Kara
601

Simple example:

In this post we cover usage of String indexOf() method with simple code examples.

Definition:

We use Java String indexOf() method to find index of specified substring or char in given String.

1. String indexOf(String str)

Edit

Output:

Why indexOf() method printed index 5? Explanation below.

2. Explanation of index

Edit

Why indexOf() method printed index 5?

Below code prints indexes of each character.

Output:

Another short explanation of index

3. String indexOf(String str, int fromIndex)

Edit

Output:

4. String indexOf(int ch)

Edit

Output:

5. String indexOf(int ch, int fromIndex)

Edit

Output:

6. Versions of String indexOf method

Edit

Java provides 4 versions of indexOf with different parameters:

  • public int indexOf(String str)

  • public int indexOf(String str, int fromIndex)

  • public int indexOf(int ch)

  • public int indexOf(int ch, int fromIndex)

The most popular version of indexOf() method is

  • public int indexOf(String str)

Post thumbnail

Edit

Post introduction image - Java - String indexOf() method example - link https://dirask.com/q/v106yp

 

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join