Categories
java String

find the last index of a character in string using java

In this post, we will learn to find the last index of a character in string using java

As like in above image we are going to take String “BeginnersBug” for our example.

This array starts from 0 index to 11.

In this example, we are going to find the last occurrence of character ‘B’. which is 9

Note: that lastIndexOf is case sensitive. It will return -1 if the character is not found

Syntax
s.lastIndexOf('B');
Example
public class LastIndex {

	public static void main(String[] args) {
		try {
			String s = "BeginnersBug";
			int indexOf = s.lastIndexOf('B');
			System.out.println("The last index of B is " + indexOf);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
Output
The last index of B is 9
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/strings/LastIndex.java

Related Articles

find the first occurrence of a character in string using java

Convert String to lowercase using java

Leave a Reply

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