Hello folks,

In this post, we will learn how to replace a sub string in a given string. Detailed description of problem is given below:

You have an input string and you need to replace sub string from new sub string in given input string.


Input: This bike is my bike.
Action: Replace “bike” from “car.
output: This car is my car.


  1. We will use split method and will split given input string from word which needs to be replaced. Remember, how split works.
  2. After split, add new word at end of all split word except last one.
  3. First verify if last word in input string is equal to string to be replaced. If yes then append new word.


package MakeSeleniumEasy;

import java.util.Scanner;

public class ReplaceString 
    public static void main(String[] args)
    	Scanner sc= new Scanner(System.in);
		System.out.println("Enter string:");
		String inputString= sc.nextLine();
		System.out.println("Enter word to be replaced from:");
		String from= sc.nextLine();
		System.out.println("Enter word to replaced to:");
		String to= sc.nextLine();
		System.out.println("Input string: "+inputString);
		System.out.println("Word to be replaced from: "+from);
		System.out.println("Word to be replaced to: "+to);
        String replacedString = replaceMethod(inputString, from, to);

    static String replaceMethod(String str, String from, String to) 
    	// We will split string array using wordToBeReplaced. Splitter word will not be stored.
        String[] arr = str.split(from);
        StringBuilder output = new StringBuilder();

        //Concatenating new word in last of word of each index except last index
        // We can not add new word for last indexed word as we are not sure we need to add or not.
        int i = 0;
        for (; i < arr.length - 1; i++)

        // Verifying if last word of given string is similar to word to be replaced/
        // If yes, then append new word otherwise don't. 

        return output.toString();


Enter string:
this car is my car
Enter word to be replaced from:
Enter word to replaced to:
Input string: this car is my car
Word to be replaced from: car
Word to be replaced to: bike
this bike is my bike
Enter string:
Enter word to be replaced from:
Enter word to replaced to:
Input string: thiscarismycar
Word to be replaced from: car
Word to be replaced to: bike

I have put comments wherever it is necessary. If you have any doubt, you can ask me.


2 thoughts on “Java Program 10: Replace All Occurrence Of Sub string In Given String

  1. public class ReplaceAllDemo {
    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter string:”);
    String inputString = sc.nextLine();
    System.out.println(“Enter word to be replaced from:”);
    String from = sc.nextLine();
    System.out.println(“Enter word to replaced to:”);
    String to = sc.nextLine();
    System.out.println(“Input string: ” + inputString);
    System.out.println(“Word to be replaced from: ” + from);
    System.out.println(“Word to be replaced to: ” + to);

    inputString = inputString.replaceAll(from, to);

Leave a Reply