Languages
[Edit]
EN

Java - find character index in string

0 points
Created by:
Emerson-V
333

In this article, we would like to show you how to find a character index in string in Java.

Quick solution:

String text = "Dirask is awesome!";

int result = text.indexOf("D");    // 0

System.out.println(result);

 

Practical example

In this example, we use String.indexOf() method to find character index in the text string.

public class Example {

    public static void main(String[] args) {
        String text = "Dirask is awesome!";

        int index1 = text.indexOf("D");         // 0
        int index2 = text.indexOf("a");         // 3    (first occurrence)

        int index3 = text.indexOf("Dirask");    // 0
        int index4 = text.indexOf("is");        // 7

        System.out.println(index1);  // 0
        System.out.println(index2);  // 3    (first occurrence)

        System.out.println(index3);  // 0
        System.out.println(index4);  // 7
    }
}

Output:

0
3
0
7
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.

Java - String (popular problems)

Java - find character index in string
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