Java program to get the last index of any given character in a string

In this java program, we are getting the last index of any given character in a string? Here we are taking a string and character; we are printing its last index in string.
Submitted by IncludeHelp, on November 25, 2017

Given a string and we have to find last index of any given character in string using Java program.


    Enter string: IncludeHelp
    Enter character: l
    Last index of l is: 9


This is a method of String class, it returns the last index of given character, and character will be passed through the parameter.

Program to find last index of any character in string in Java

import java.util.Scanner;

public class StringLastValue 
	public static void main(String[] arg)
		// create object of string and scanner class.
		String S;
		Scanner SC=new Scanner(;

		// enter the string.
		System.out.print("Enter the string : ");
		int index = 0;      

		// enter the element for last occurence.
		index = S.lastIndexOf('l');         

		System.out.println("Last index is : " +index);


Enter the string : IncludeHelp
Last index is : 9

Java String Programs »

Related Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.