How to find location of word and character from string?

In this blog, we will see how to find location of word and character from string. We will use indexOf() and lastIndexOf method. Both methods will return location of word and character.

How to find location of word and character from string program

class str5

{

                public static void main(String args[])

                {

                                String str = “Welcome in Java String”;

 

                                // let’s finds location of “in” from string

                                System.out.println(str.indexOf(“in”));   // return location of “in” -> At 8 place

 

                                // Let’s finds location of ‘i’ from string

                                System.out.println(str.indexOf(‘i’));        // return location of ‘i’ -> At 8 place

 

                                // Let’s find location of “in” from String

                                System.out.println(str.lastIndexOf(“in”));            // return location of “in” from last

 

                                // Let’s find location of ‘i’ from string

                                System.out.println(str.lastIndexOf(‘i’)); // return location of ‘i’ from last

                }

}


Output:

8

8

19

19

 

Video on IndexOf() and lastIndexOf()


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

233total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

EmailEmail
PrintPrint