Solved

Java substring replacement

Posted on 2008-10-01
6
898 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace a word with other 1 44
Should localization be done inside spring controller 5 25
difference of if loops 23 49
arguments to jar 5 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 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