[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Java Replace String

I want to do find and replace. For example we have a string with something like .
"  select * from table a, table b where
a.id = b.id
/*<input>
and
a.input = ?
<input>*/   "

Now i want tofind all /*<input>  and <input>*/ and replace it with blank space or remove it.
0
shetty01
Asked:
shetty01
1 Solution
 
Gurvinder Pal SinghCommented:

String str="select * from table a, table b where a.id = b.id /*<input> and a.input = ? <input>*/  ";
System.out.println(str.replace("<input>","\\s"));
0
 
Gurvinder Pal SinghCommented:
String str="select * from table a, table b where a.id = b.id /*<input> and a.input = ? <input>*/  ";
System.out.println(str.replaceAll("<input>","\\s"));
0
 
vkphoenixfrCommented:
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);     :)

Phoenix
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ValeriCommented:
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 :
System.out.println(str.replaceAll("/\\*<input>","").replaceAll("<input>\\*/",""));
0
 
CEHJCommented:
Try the following:
s = s.replaceAll("(?sm)(/\\*<input>.*?<input>\\*/)", "");	

Open in new window

0
 
CEHJCommented:
btw, the dup question you posted on this should be deleted
0
 
shetty01Author Commented:
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.
0
 
CEHJCommented:
Please post then the code you're actually using. (The code you refer to won't work as is)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now