ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.