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_ch_index.htm below:

Java.lang.String.lastIndexOf() Method

Java.lang.String.lastIndexOf() Method Description

The java.lang.String.lastIndexOf(int ch, int fromIndex) method returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.

Declaration

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

public int lastIndexOf(int ch, int fromIndex)
Parameters Return Value

This method returns the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.

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 str = "This is tutorialspoint";
   
      /* returns positive value(last occurrence of character t) as character
         is located, which searches character t backward till index 14 */
      System.out.println("last index of letter 't' =  "
         + str.lastIndexOf('t', 14)); 
      
      /* returns -1 as character is not located under the give index,
         which searches character s backward till index 2 */
      System.out.println("last index of letter 's' =  "
         + str.lastIndexOf('s', 2)); 
      
      // returns -1 as character e is not in the string
      System.out.println("last index of letter 'e' =  "
         + str.lastIndexOf('e', 5));
   }
}

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

last index of letter 't' = 10
last index of letter 's' = -1
last index of letter 'e' = -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