Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java substring replacement

Posted on 2008-10-01
6
Medium Priority
?
923 Views
Last Modified: 2013-11-23
I am looking for a good method for doing substring replacement.
For example, the string is "Experts collaborate with you to find solutions! Make their job easier by clearly describing your issue with specific information." and I want to replace the substring "you" to let say "Jack Bauer". I was wondering which method is the best! Please show code example! Thanks!
0
Comment
Question by:alfredng1
6 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22619880
const string varToRemove = "you";
const string varToRemove2 = "You";
const string varToReplace = "Jack Bauer";

string.replace(varToRemove, varToReplace);

string.replace(varToRemove2, varToReplace);
0
 
LVL 12

Expert Comment

by:basav_com
ID: 22619881
You need to use  'replace' method of String.
Here is the example:
  String str="How are you";
  str = str.replace("you","John");
  System.out.println(str);
0
 
LVL 12

Expert Comment

by:basav_com
ID: 22619885
Same solution at the same time ;)
ID:22619880Author:RishadanPortDate:10.02.2008 at 04:23AM IST
ID:22619881Author:basav_comDate:10.02.2008 at 04:23AM IST

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Assisted Solution

by:verickson
verickson earned 400 total points
ID: 22619919


Use the Pattern and Matcher classes (import java.util.regex.*;)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html 


String str = "You are sad.";
System.out.println("Before:" + str);
 
Pattern pattern = Pattern.compile("sad"); // Pattern is "sad"
Matcher match = pattern.matcher(str);     // Apply the pattern to str
 
String newStr = match.replaceAll("happy");  // Replace "sad" with "happy" and store in newStr
System.out.println("After: " + newStr);

Open in new window

0
 
LVL 9

Accepted Solution

by:
mbodewes earned 600 total points
ID: 22619966
It depends a bit on what kind of functionality you are looking for. You probably would not want to replace "you" in "your" with Jack Bauer, whoever that is.

So then you would have to do something like:



	final String yourEx = "^(.*\\W)you(\\W.*)$";
		final Pattern yourPattern = Pattern.compile(yourEx);
		final Matcher yourMatcher = yourPattern.matcher("Experts collaborate with you to find solutions! Make their job easier by clearly describing your issue with specific information.");
		if(!yourMatcher.matches()) {
			throw new RuntimeException("No matches found, this is bad");
		}
		final String bauerStr = yourMatcher.group(1) + "Jack Bauer" + yourMatcher.group(2);
		System.out.printf("The new string is: \"%s\"%n", bauerStr);
	

Open in new window

0
 
LVL 9

Expert Comment

by:mbodewes
ID: 22619968
Checked with Eclipse and QuickREx by the way, use the right tool for the right job.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question