Java Replace String

Posted on 2010-09-12
Last Modified: 2013-12-29
I want to do find and replace. For example we have a string with something like .
"  select * from table a, table b where =
a.input = ?
<input>*/   "

Now i want tofind all /*<input>  and <input>*/ and replace it with blank space or remove it.
Question by:shetty01
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
LVL 40

Expert Comment

ID: 33657279

String str="select * from table a, table b where = /*<input> and a.input = ? <input>*/  ";
LVL 40

Expert Comment

ID: 33657304
String str="select * from table a, table b where = /*<input> and a.input = ? <input>*/  ";

Expert Comment

ID: 33660313
Use The commons library.
In your case, commons-lang, and it's StringUtils class :

StringUtils.replace(originalString, stringToReplace, stringReplacement);

Replaces all occurences of stringToReplace in originalString by stringReplacement.

So if you want to remove <input> by nothing, th line goes like that :
String newSqlQuery = StringUtils.replace(sqlQuery, "<input>", "");

More generally, have a look at commons library, they are really usefull (commons-lang, commons-io, commons-dbcp, and so on...) Go to apache commons website to see how many they have.

It allows  you to use a well written, tested and proven to be efficient set of very usefull functions.

Who nowadays closes an IO stream without using IOUtils.closeSilentlt(ioStream);     :)

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 16

Expert Comment

ID: 33660331
Does the /*<input> and <input>*/ are considered to be something like start and end tag? I think they are, so try this, because '*' must be avoided in this way :
LVL 86

Expert Comment

ID: 33660843
Try the following:
s = s.replaceAll("(?sm)(/\\*<input>.*?<input>\\*/)", "");	

Open in new window

LVL 86

Expert Comment

ID: 33660852
btw, the dup question you posted on this should be deleted

Author Comment

ID: 33664250
I found the solution. Somehow i posted it twice since first time my post did not work.  I got solution in my other post. thanks for help.
LVL 86

Accepted Solution

CEHJ earned 500 total points
ID: 33664323
Please post then the code you're actually using. (The code you refer to won't work as is)

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:

734 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