YouTip LogoYouTip

String Last Occurance

## Java String: Find the Last Occurrence of a Substring In Java programming, locating the position of a specific character or substring within a larger string is a common task. To find the **last** occurrence of a substring, Java provides the built-in `lastIndexOf()` method of the `String` class. This tutorial explains how to use `lastIndexOf()` with clear syntax explanations, practical code examples, and key considerations. --- ### Syntax and Usage The `lastIndexOf()` method searches backward from the end of the string (or from a specified starting index) and returns the index of the last occurrence of the specified character or substring. #### Method Signatures The `String` class provides four overloaded variants of `lastIndexOf()`: 1. **Find last occurrence of a substring:** ```java public int lastIndexOf(String str) ``` 2. **Find last occurrence of a substring, searching backward from a specific index:** ```java public int lastIndexOf(String str, int fromIndex) ``` 3. **Find last occurrence of a character (Unicode code point):** ```java public int lastIndexOf(int ch) ``` 4. **Find last occurrence of a character, searching backward from a specific index:** ```java public int lastIndexOf(int ch, int fromIndex) ``` #### Return Value * **`int`**: The 0-based index of the last occurrence of the character or substring. * **`-1`**: Returned if the character or substring is not found. --- ### Code Example The following example demonstrates how to find the last occurrence of the substring `"YouTip"` (originally `"Runoob"` in the source) within a target string. #### `SearchLastString.java` ```java public class SearchLastString { public static void main(String[] args) { // Define the original string String strOrig = "Hello world, Hello YouTip"; // Find the last occurrence of the substring "YouTip" int lastIndex = strOrig.lastIndexOf("YouTip"); // Check if the substring was found if (lastIndex == -1) { System.out.println("Substring 'YouTip' not found."); } else { System.out.println("The last occurrence of 'YouTip' is at index: " + lastIndex); } } } ``` #### Output ```text The last occurrence of 'YouTip' is at index: 19 ``` --- ### Key Considerations 1. **Zero-Based Indexing:** Java strings use 0-based indexing. In the example above, the character `'Y'` in the last occurrence of `"YouTip"` is located at index `19`. 2. **Case Sensitivity:** The `lastIndexOf()` method is case-sensitive. Searching for `"youtip"` in `"Hello YouTip"` will return `-1`. 3. **Handling `fromIndex`:** When using `lastIndexOf(String str, int fromIndex)`, the search moves backward starting from `fromIndex`. Any occurrence of the substring at or before `fromIndex` will be considered, while occurrences after `fromIndex` are ignored. 4. **Null Pointer Exception:** If the source string (`strOrig`) or the search parameter (`str`) is `null`, the method will throw a `NullPointerException`. Always ensure your strings are initialized before performing searches.
← String Removing CharString Compare β†’