• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string search & replace

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic