Java #33: How to Replace A String in a Sentence in Java

string.replace(“Old Word”, “New Word”)

//Full Program Output

//—————Full Program————-
//Programmer: Akinwale Owi
//Company: Dagba Computers
//Program Purpose: Replace Strings in sentences

package dagbacomputers;

* @author Akin

import java.util.Scanner;

public class DagbaComputers {

* @param args the command line arguments
public static void main(String[] args)
Scanner scanReader = new Scanner(;

//Asking for sentence we will search and replace
System.out.println(“Please enter the sentence: “);

//String reading in answer
String sentence = scanReader.nextLine();

//Printing out what we just read in
System.out.println(“You just entered: ” + sentence);

//Replacing the word
sentence = sentence.replace(“Microsoft”, “Dagba”); //Format is string.replace(“Old word”, “New Word”)

//Printing the new sentence
System.out.println(“New sentence: ” + sentence);

}//end of main

}//End of DagbaComputers Class

//———–End of program—————–

//Screenshot of full program Source Code

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s