File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Replace String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replace String" Watch "Replace String" New topic

Replace String

Edward Durai
Ranch Hand

Joined: Oct 09, 2004
Posts: 223
Consider i have string like

"select [columnName] from tablename"

I want to replace [columnName] to [UserName]

finally, i need "select [UserName] from tablename";


Thank You<br />Edward
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Use regular expression. Take a look at Pattern and Matcher classes.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41621
Something like the following may do the trick. You'll need to add a loop to deal with the case when findStr can occur more than once in targetStr. (The indices may be off, but you get the idea.)

Ping & DNS - my free Android networking tools app
Rahim Vindhani

Joined: Dec 06, 2005
Posts: 10
isnt it as simple as

if its more than one time, You can use replaceAll (only JDK 1.4 and later I guess)

Rahim Vindhani,<br /><a href="" target="_blank" rel="nofollow">Java Examples</a>
Consider Paul's rocket mass heater.
subject: Replace String