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

//Format
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
*/

//imports
import java.util.Scanner;
import java.io.*;

public class DagbaComputers {

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

//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:

WordPress.com Logo

You are commenting using your WordPress.com 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

Blog at WordPress.com.

Up ↑

%d bloggers like this: