Solved

Java substring replacement

Posted on 2008-10-01
6
896 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now