How to Replace the Last Occurrence of a Substring in a String in Java? Last Updated : 29 Jan, 2024 Comments Improve Suggest changes 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 Java Program to find the Last Index of a Particular Word in a String To find out the last occurrence of a character or string from a given string across which the substring or character is been searched over and returning the index value of the character or substring is found. Real-life Example: Consider an example of a book. The book contains pages where pages conta 4 min read Java Program To Check If A String Is Substring Of Another Write a java program for a given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples : Input: s1 = "for", s2 = "geeksforgeeks"Output: 5Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 3 min read Java Program to Find Occurrence of a Word Using Regex Java's regular expressions, or regex, let you do advanced text manipulation and matching. Regex offers a handy approach for searching for a term in a text wherever it appears. In this article, we will learn to find every occurrence of a word using regex. Program to Find Occurrence of a Word Using Re 2 min read How to find the last index using regex of a particular word in a String? To find the last index of a particular word in a string using regular expressions in Java, you can use the Matcher class along with a regular expression that captures the desired word. Java Program to last index Using the Regex of a Particular Word in a StringBelow is the implementation of the last 2 min read Java Program to Search a Particular Word in a String Using Regex In Java string manipulation, searching for specific words is a fundamental task. Regular expressions (regex) offer a powerful and flexible approach to achieve this search. It matches the patterns simply by comparing substrings. In this article, we will learn how to search for a particular word in a 3 min read Like