Solved

Java substring replacement

Posted on 2008-10-01
6
899 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Assisted Solution

by:verickson
verickson earned 100 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 150 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
hibernate insert example 13 34
Glassfish admin console not working 1 32
Bot application - advice 3 38
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

809 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