Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Java lastIndexOf Method

by suresh

The Java lastIndexOf Method is one of the Java String Methods, which is to return the index position of the last occurrence of a specified string. If the specified string not found, the lastIndexOf Method will return -1.

In this article, we will show how to use String lastIndexOf in Java Programming language with example. The syntax of the string lastIndexOf in Java Programming language is as shown below.

Java lastIndexOf Method syntax

The Java Programming Language provides four different string lastIndexOf methods to return the index position of the last occurrence.

The following Java lastindexof method will accept the character as the parameter and return the index position of the last occurrence of the specified character.

public int lastIndexOf(int ch); // It will return Integer 

//In order to use in program
String_Object.lastIndexOf(int ch)

This following method will accept the character as the first parameter and last index position (End_index) as the second argument. Java String lastIndexOf method will start looking from index position 0 to the End_index index position and returns the last occurrence of the specified character.

public int lastIndexOf(int ch, int End_Index); // It will return Integer 

//In order to use in program
String_Object.lastIndexOf(int ch, int End_Index)

The following Java lastindexof method will accept the string data as the argument and return the index position of the last occurrence of the specified substring.

public int lastIndexOf(String str); // It will return Integer 

//In order to use in program
String_Object.lastIndexOf(String str)

The below lastIndexOf method will accept the string data as the first parameter and last index position (End_index) as the second argument.

Java String.lastIndexOf method will start looking from index position 0 to the End_index index position and returns the last occurrence of the specified substring.

public int lastIndexOf(String str, int End_Index); // It will return Integer 

//In order to use in program
String_Object.lastIndexOf(String str, int Starting_Index)
  • String_Object: Please specify the valid String Object.
  • End_Index: Please specify the ending index position. If you specify this value, the string.lastIndexOf function will start looking from index position 0 to this position (not to the end).

NOTE: If you specify the negative value as the End_Index, the Java String.lastIndexOf function will return -1.

Java lastIndexOf Method Example

The Java string lastIndexOf method returns the index position of the last occurrence of a specified string. This program will help to understand the string lastIndexOf method.

//Java String lastIndexOf example
package StringFunctions;

public class LastIndexOfMethod {
	public static void main(String[] args) {
		String str = "Tutorials On Java Programming";
		String str1 = "We are abc working at abc company";
		
		int a = str.lastIndexOf('i');
		int b = str.lastIndexOf('i', 10);
		int c = str.lastIndexOf('i', -10);
		int d = str.lastIndexOf('y');
		
		int e = str1.lastIndexOf("abc");
		int f = str1.lastIndexOf("abc", 21);
		int g = str1.lastIndexOf("abc", 29);
		int h = str1.lastIndexOf("abc", -25);
		
		System.out.println( "Last Index position of i = " + a);
		System.out.println( "Starting from 10 - Last Index position of i = " + b);
		System.out.println( "Start from -Ve Index - Last Index position of i = " + c);
		System.out.println( "Last Index position of y = " + d);
		System.out.println( "Last Index position of String abc = " + e);
		System.out.println( "Starting from 10 - Last Index position of String abc = " + f);
		System.out.println( "Starting from 25 - Last Index position of String abc = " + g);
		System.out.println( "Start from -Ve Index - Last Index position of String abc = " + h);
	}
}

OUTPUT

Java lastIndexOf Method 1

ANALYSIS

Within this Java lastindexof, the following statement call the public int lastIndexOf (int ch) method to find the index position of last occurred ‘i’

int a = str.lastIndexOf('i');

The following Java string lastIndexOf statement calls the public int lastIndexOf (int ch, int End_Index) method to find the index position of ‘i’ starting from index position 0 to index position 10. As we all know, i is located only once before the index position 10. So, the lastindexof function has returned the same.

int b = str.lastIndexOf('i', 10);

Next, we used the negative value as the End_Index.

int c = str.lastIndexOf('i', -10);

In the next line, we are looking for non-existing item ‘y’ inside the str. It will call the public int lastIndexOf (int ch)method to find the last index position of ‘y’ since it does not find, it is returning -1 as output.

int d = str.lastIndexOf('y');

It calls the public int lastIndexOf (String str) method to find the index position of a substring ‘abc’ and store the last index value in variable e.

int e = str1.lastIndexOf("abc");

NOTE: You should count the space as One Character in the lastIndexOf String Method.

From the above statement, although the term abc repeated multiple times, Java string lastIndexof function is returning the index position of the last occurrence. Now, let us provide the end index position.

The following statement will call a public int lastIndexOf(String str, int End_Index) method to return the last occurrence of a string abc starting at index position 0 to 21.

int f = str1.lastIndexOf("abc", 21);

Let us change the End_Index value to 29. It means lastIndexOf method returns the last occurrence of string abc starting at index position 0 to 29.

int g = str1.lastIndexOf("abc", 29);

Lastly, we used System.out.println statements to print the string lastIndexOf methods output.

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy