- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java.lang.String.lastIndexOf() Method
Description
The java.lang.String.lastIndexOf(String str) method Returns the index within this string of the rightmost occurrence of the specified substring. The rightmost empty string "" is considered to occur at the index value this.length().
The returned index is the largest value k such that this.startsWith(str, k) is true.
Declaration
Following is the declaration for java.lang.String.lastIndexOf() method
public int lastIndexOf(String str)
Parameters
str − This is the substring to search for.
Return Value
If the string argument occurs one or more times as a substring within this object, then the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.
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"; // returns index of first character of the last substring "tutorials" System.out.println("index = " + str1.lastIndexOf("tutorials")); // returns -1 as substring "admin" is not located System.out.println("index = " + str1.lastIndexOf("admin")); } }
Let us compile and run the above program, this will produce the following result −
index = 28 index = -1
To Continue Learning Please Login
Login with Google