permaculture playing cards
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: 42965
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.)
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>
I agree. Here's the link:
subject: Replace String
It's not a secret anymore!