How to Replace the Last Occurrence of a Substring in a String in Java? Last Updated : 29 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn about replacing the last instance of a certain substring inside a string as a typical need. We'll look at a practical Java solution for this in this post. Replace the last occurrence of a Substring in a String in JavaWe may use the lastIndexOf() method to determine the location of the last occurrence of a substring in a Java string, and then we can use the substring() method to build a new string with the replacement. The substrings that come before and after the target substring are then concatenated. An example of the above-mentioned topic is given below: Java // Java Program Replace the last // Occurrence of a Substring in a String // Driver Class public class ReplaceLastOccurrenceExample { public static String replaceLastOccurrence(String original, String target, String replacement) { int lastIndex = original.lastIndexOf(target); if (lastIndex == -1) { // Target substring not found return original; } String before = original.substring(0, lastIndex); String after = original.substring(lastIndex + target.length()); return before + replacement + after; } // Main Function public static void main(String[] args) { String inputString = "Hello world, how's the world today?"; String targetSubstring = "world"; String replacementSubstring = "universe"; String result = replaceLastOccurrence(inputString, targetSubstring, replacementSubstring); System.out.println("Original String: " + inputString); System.out.println("String after replacement: " + result); } } OutputOriginal String: Hello world, how's the world today? String after replacement: Hello world, how's the universe today? Explanation of the above Program:Three arguments are required for the replaceLastOccurrence method: the original string, the target substring that has to be changed, and the replacement substring.To determine the location of the target substring's last occurrence, it uses lastIndexOf().The original text is returned unaltered if the desired substring cannot be located (lastIndex is -1).substring() is then used to extract the substrings that come before and after the target substring.In order to create the updated string, it concatenates these substrings with the replacement substring. Comment More infoAdvertise with us Next Article How to Replace the Last Occurrence of a Substring in a String in Java? R rahul9yw89 Follow Improve Article Tags : Java Java Programs Java-Strings Java Examples Practice Tags : JavaJava-Strings Similar Reads How to Replace the First Occurrence of a String Using Regex in Java? Regex is a very interesting way to search for patterns in a String that the user provides. Regex stands for Regular Expressions. It consists of some patterns that can be planned and modified according to the usage of the program. In this article, we will discuss how to replace the first occurrence o 2 min read How to Split a String into Equal Length Substrings in Java? In Java, splitting a string into smaller substrings of equal length is useful for processing large strings in manageable pieces. We can do this with the substring method of the loop. This method extracts a substring of the specified length from the input string and stores it in a list.Example:In the 3 min read How to Replace All Occurings of String Using Regex in Java? Regex in Java is an interesting way to search for patterns in a string that the user provides. Expanded as Regular Expressions, It consists of some patterns that can be planned and modified according to the usage in the program. Example of Replace All Occurings of a StringInput: str="This is a sampl 2 min read Split a String into a Number of Substrings in Java Given a String, the task it to split the String into a number of substrings. A String in java can be of 0 or more characters. Examples : (a) "" is a String in java with 0 character (b) "d" is a String in java with 1 character (c) "This is a sentence." is a string with 19 characters. Substring: A Str 5 min read Replace all occurrences of a string with space Given a string and a substring, the task is to replace all occurrences of the substring with space. We also need to remove trailing and leading spaces created due to this. Examples: Input: str = "LIELIEILIEAMLIECOOL", sub = "LIE" Output: I AM COOL By replacing all occurrences of Sub in Str with empt 11 min read Like