• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string search & replace

 
Chitra Jay
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Pls help me with this issue.
Contents of the text file:
bla bla bla %s
bla bla bla bla

...more contents here...
%i bla bla %s
--end of text file.

I would like to replace the %s,%i,%s with 3 values i pass in the arguments.
eg. if send 1,2,3 then contents of the text file should have %s replaced with 1, %i replaced with 2 and %s replaced with 3. (in the order they appear in the file). I dont need to bother with whether its %s or %i, but only i need to replace %s or %i with the parameters i pass. I can read the contents of the file into a string. but how to replace values in the order specified.
 
Fintan Quill
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you could use a Collection class with key/value pairs where %s is a key and 1 is its value, or possibly vice-versa. I could be misinterpreting your dilemma however!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could call the String.replaceFirst() method three different times with the same regex and different repacement Strings


[ April 20, 2006: Message edited by: Garrett Rowe ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"CJ,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name -- like Chitra Jay, for example.

You can edit your name here.

Thank you for your prompt attention!

-Marc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic