Java String lastIndexOf() Example wpaccuracy

please click here for more wordpress cource

Sure, here’s an example of using the lastIndexOf() method in Java:

String str = "Hello World!";
int lastIndex = str.lastIndexOf("o");
System.out.println("Last index of 'o' in the string is: " + lastIndex);

In this example, we have a string “Hello World!” and we want to find the last index of the character “o”. We use the lastIndexOf() method of the String class to find the index of the last occurrence of the character “o” in the string. The result is stored in the integer variable “lastIndex”. Finally, we print the value of “lastIndex” using the System.out.println() method.

The output of the above code will be:

Last index of 'o' in the string is: 7

Here, the last occurrence of the character “o” is at index 7 in the string “Hello World!”.

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *