A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/lang/string_lastindexof_string_index.htm below:

Java.lang.String.lastIndexOf() Method

Java.lang.String.lastIndexOf() Method Description

The java.lang.String.lastIndexOf(String str, int fromIndex) method returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. The integer returned is the largest value k such that −

k >= Math.min(fromIndex, this.length()) && this.startsWith(str, k), if no such value of k exists, then -1 is returned.

Declaration

Following is the declaration for java.lang.String.lastIndexOf() method

public int lastIndexOf(String str, int fromIndex)
Parameters Return Value

This method returns the index within this string of the last occurrence of the specified substring.

Exception

NA

Example

The following example shows the usage of java.lang.String.lastIndexOf() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "Collections of tutorials at tutorials point";
     
      /* search starts from index 17 and if located it returns 
         the index of the first character of the last substring "tutorials" */
      System.out.println("index = " + str1.lastIndexOf("tutorials", 17)); 

      /* search starts from index 9 and returns -1 as substring "admin"
         is not located */
      System.out.println("index = " + str1.lastIndexOf("admin", 9));     
   }
}

Let us compile and run the above program, this will produce the following result −

index = 15
index = -1

java_lang_string.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4