Home » Java programs » Java string programs

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(System.in);

		// 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

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.