×

Java Programs

Java Practice

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.

Example:

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

String.lastIndexOf()

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 : ");
		S=SC.nextLine();
		int index = 0;      

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

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

Output

Enter the string : IncludeHelp
Last index is : 9

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.