lastIndexOf() String Example

lastIndexOf() String Example


Many methods exist in java.lang.String class used for different string manipulations like checking the availability of a character, to extract a part of a string or to convert a data type to string etc. Present topic is on method lastIndexOf() String.

lastIndexOf() method checks the existence of a single character or a group of characters (string) in the string.

lastIndexOf() in String class is overloaded 4 times. Following are the method signatures.

  1. public int lastIndexOf(int ch): Returns the index number of the specified character ch (or character in ASCII number, see notes after example) passed as parameter. The availability of the character ch is checked from right to left starting from last index number in the string. The string may contain multiple ch characters. Last occurrence of the ch in the string is returned. After returning the index number of the last occurrence, further checking of ch occurrence is stopped. If the character ch is not found in the string, -1 is returned. Or to say simply, returns the last occurrence of ch in the string.
  2. public int lastIndexOf(int ch, int fromIndex): In brief, the difference from the previous method is the checking position; earlier it was last position and now it is from fromIndex. Read further. Returns the index number of the specified character ch passed as parameter. The availability of the character ch is checked from right to left starting from fromIndex number in the string (in the previous method, availability is checked from last index position). The string may contain multiple ch characters. Last occurrence of ch in the string from fromIndex position is returned. After returning the index number of the last occurrence, further checking of ch occurrence is stopped. If the character ch is not found in the string, -1 is returned.
  3. public int lastIndexOf(String str): Previous method takes a single character as parameter. But this overloaded method takes string as parameter. The last occurrence of the str is checked in the actual string from last index number and if found, returns its index number. The availability is checked from right to left and if not found, returns -1.
  4. public int lastIndexOf(String str, int fromIndex): Returns the index number of the specified string str passed as parameter. The availability of the string str is checked from right to left, but starting from fromIndex number in the string (in the previous method, availability is checked from last index position). The actual string may contain multiple str strings. Last occurrence of str in the actual string from fromIndex position is returned. After returning the index number of the last occurrence, further checking of str occurrence is stopped. If the string str is not found, -1 is returned.

Note: Like equals() and indexOf() methods, lastIndexOf() checking is also case-sensitive.

Following example illustrates the usage of 4 overloaded methods of lastIndexOf() String.


lastIndexOf() String
Output screenshot on lastIndexOf() String Example

int x = str.lastIndexOf('B');

In the above statement, B can be replaced by its ASCII value 66 as follows.

int x = str.lastIndexOf(66);

Observe keenly the signature of the first overloaded method; the argument is int value and not char. Actually, when we pass char, char is converted to ASCII number and then evaluated.

4 thoughts on “lastIndexOf() String Example

Leave a Reply

Your email address will not be published. Required fields are marked *