This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 41182
    
  45
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
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://aspose.com/file-tools
 
subject: Replace String
 
Similar Threads
Array index out of bounds exception
Taking entries from database containing %
FOR UPDATE STATEMENT
Taking entries from database containing %
invalid handle--preparedStatement.setString()