Saturday, 26 July 2014

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

Structure of the Problem Requirements 

In this Problem we learn how to replace a string with another string in Java. First we initialize am empty string and a string with sentence with replace the first string which we receive from user . Just use Java builtin function which receive two parameter of string . First String is the actual 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 (;
        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
Java String Programming

Share it Please

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.