aspose file tools
The moose likes Beginning Java and the fly likes Replace String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Replace String" Watch "Replace String" New topic
Author

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";

how?


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
Marshal

Joined: Mar 22, 2005
Posts: 35253
    
    7
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.)


Android appsImageJ pluginsJava web charts
Rahim Vindhani
Greenhorn

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="http://www.java-examples.com" target="_blank" rel="nofollow">Java Examples</a>
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Replace String
 
Similar Threads
FOR UPDATE STATEMENT
Taking entries from database containing %
invalid handle--preparedStatement.setString()
Array index out of bounds exception
Taking entries from database containing %