Table of Contents
lastIndexOf method can take char or string as argument and also you can provide fromIndex also.
Methods:
public int lastIndexOf(int ch)
returns index of last occurrence of character within String
public int lastIndexOf(int ch,int fromIndex)
returns index of last occurrence of character within String, going backward from specific index “fromIndex”
public int lastIndexOf(String)
returns index of last occurrence of substring within String
public int lastIndexOf(int ch,int fromIndex)
returns index of last occurrence of substring within String, going backward from specific index “fromIndex”
All lastIndexOf methods return -1 if char or substring is not present in the String
String indexOf Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package org.arpit.java2blog; public class StringLastIndexOfExample { /* * @ Author : Arpit Mandliya */ public static void main(String args[]) { String str="Hello world from java2blog"; // Using lastIndexOf method with character System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with character"); System.out.println("---------------------------------------------------"); System.out.println("Last index of character l in "Hello world from java2blog" is : "+str.lastIndexOf('l')); System.out.println(); String str1="from"; System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with fromIndex with character"); System.out.println("---------------------------------------------------"); System.out.println("Last index of character o from index 6 in "Hello world from java2blog" is : "+str.lastIndexOf('o',6)); System.out.println(); // Using lastIndexOf method with String System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with String"); System.out.println("---------------------------------------------------"); System.out.println("Last index of String "from" in "Hello world from java2blog" is : "+str.lastIndexOf(str1)); System.out.println(); String howDo="How do you do"; System.out.println("---------------------------------------------------"); System.out.println("Using lastIndexOf method with fromIndex with String"); System.out.println("---------------------------------------------------"); System.out.println("Last index of String "do" from index 9 in "How do you do" is : "+howDo.lastIndexOf("do",9)); System.out.println(); System.out.println("---------------------------------------------------"); System.out.println("If String or char is not available"); System.out.println("---------------------------------------------------"); System.out.println("Last index of "from" in "How do you do" is : "+howDo.lastIndexOf("from")); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
-------------------- --------------------------------------------------- Using lastIndexOf method with character --------------------------------------------------- Last index of character l in "Hello world from java2blog" is : 23 --------------------------------------------------- Using lastIndexOf method with fromIndex with character --------------------------------------------------- Last index of character o from index 6 in "Hello world from java2blog" is : 4 --------------------------------------------------- Using lastIndexOf method with String --------------------------------------------------- Last index of String "from" in "Hello world from java2blog" is : 12 --------------------------------------------------- Using lastIndexOf method with fromIndex with String --------------------------------------------------- Last index of String "do" from index 9 in "How do you do" is : 4 --------------------------------------------------- If String or char is not available --------------------------------------------------- Last index of "from" in "How do you do" is : -1 |