Solved

Java Replace String

Posted on 2010-09-12
8
354 Views
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.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
Comment
Question by:shetty01
[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
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33657279

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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33657304
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
 
LVL 1

Expert Comment

by:vkphoenixfr
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);     :)

Phoenix
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Valeri
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 :
System.out.println(str.replaceAll("/\\*<input>","").replaceAll("<input>\\*/",""));
0
 
LVL 86

Expert Comment

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

Open in new window

0
 
LVL 86

Expert Comment

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

Author Comment

by:shetty01
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.
0
 
LVL 86

Accepted Solution

by:
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)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 84
jdbc error in jsp application 20 74
arguments to jar 5 40
Java array sort 10 34
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …

733 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