Solved

Java substring replacement

Posted on 2008-10-01
6
905 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 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