How to replace a string a string with another string in java

In this Problem we learn how to replace a string with another string in Java. First we initialize an empty string and a string with sentence. Just use Java builtin function which receive two parameter of string. First String is the empty string and the Second string is that which is replaced.

Source Code  


package com.ancodingpoint.ww;
import java.util.Scanner;

public class String_Replacement {

   
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        String sentence = "";
        String b = " This is the Second string which replace the first one! ";
        System.out.println(" Enter Your Story ...");
        sentence = input.nextLine();
        System.out.println(sentence.replaceAll(sentence, b));
       
    }
    
}

Output of the Program 

Java String Programming

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments: