Java – StringBuffer lastIndexOf() method

In this article, we will discuss StringBuffer’s lastIndexOf() method which returns last occurrence of specified sub-string

1. StringBuffer’s lastIndexOf() method:

  • This method is used to get last occurrence of sub-string i.e.; index-position of last occurrence of specified sub-string
  • Note: There are 2 variants or overloaded lastIndexOf() methods

1.1 Method Signature:

1
2
3
public int lastIndexOf(String str);
 
public int lastIndexOf(String str, int fromIndex);

1.2 Parameters:

  • str                –> sub-string to be searched, to get last occurrence
  • fromIndex  –> position from where to start searching

1.3 Returns:

lastIndexOf() method

Returns

lastIndexOf(String str);Returns last occurrence of specified sub-string (i.e.; index-position for last occurrence)
lastIndexOf(String str, int fromIndex);Returns last occurrence of specified sub-string, starting from specified index (i.e.; index-position for last occurrence)

1.4 Throws:

  • NullPointerException, if specified sub-string is null

2. Examples on lastIndexOf() method:

2.1 To find last occurrence of specified sub-string

  • Below illustration depicts how to get last occurrence of specified sub-string

Method signature:

1
public int lastIndexOf(String str);

StringBufferLastIndexOfMethod.java

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
package in.bench.resources.stringbuffer.methods;
 
public class StringBufferLastIndexOfMethod {
 
    /**
     * lastIndexOf() - starts searching from backward
     * prints index-position from left-to-right
     * very similar to indexOf() method
     * @param args
     */
    public static void main(String[] args) {
 
        // StringBuffer
        StringBuffer sb = new StringBuffer(
                "East is East and West is West");
 
        // 1. To get LAST index of substring 'West'
        int lastIndexOfSubstring1 = sb.lastIndexOf("West");
 
        // print to console
        System.out.println("1. Last index of substring"
                + " 'West' is : " + lastIndexOfSubstring1);
 
        // 2. To get LAST index of substring 'East'
        int lastIndexOfSubstring2 = sb.lastIndexOf("East");
 
        // print to console
        System.out.println("2. Last index of substring"
                + " 'East' is : " + lastIndexOfSubstring2);
    }
}

Output:

1
2
1. Last index of substring 'West' is : 25
2. Last index of substring 'East' is : 8

2.2 To find last occurrence of specified sub-string starting from specified index

  • Below illustration depicts how to get last occurrence of specified sub-string, starting from specified index-position

Method signature:

1
public int lastIndexOf(String str, int fromIndex);

StringBufferLastIndexOfMethod2.java

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
package in.bench.resources.stringbuffer.methods;
 
public class StringBufferLastIndexOfMethod2 {
 
    /**
     * lastIndexOf() - starts searching from backward
     * prints index-position from left-to-right
     * very similar to indexOf() method
     * @param args
     */
    public static void main(String[] args) {
 
        // StringBuffer
        StringBuffer sb = new StringBuffer(
                "East is East and West is West");
 
        // 1. To get LAST index of substring 'East',
        // starting from 8th position
        int lastIndexOfSubstring = sb.lastIndexOf("East", 8);
 
        // print to console
        System.out.println("1. Last index of substring 'East',"
                + " starting from 8th position is  : "
                + lastIndexOfSubstring);
 
        // 2. To get LAST index of substring 'West',
        // starting from 21st position
        int lastIndexOfSubstringFrom = sb.lastIndexOf("West", 21);
 
        // print to console
        System.out.println("2. Last index of substring 'West', "
                + "starting from 21st position is : "
                + lastIndexOfSubstringFrom);
    }
}

Output:

1
2
3
4
1. Last index of substring 'East',
                            starting from 8th position is  : 8
2. Last index of substring 'West',
                            starting from 21st position is : 17

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - StringBuffer length() method
Java - StringBuffer insert() method