1

To start off, I may be using sprintf in the wrong manner.

I'm making a framework plugin that takes strings as configuration. The strings have things that need to be swapped out, for example, one string would be a path template:

[root]/[template_directory]/something/specific/[theme_name].htm

The example above is pretty specific and has a lot of variables to be swapped out.

For less variables, I've been doing it like so:

sprintf('%s/some/file/path/theme.htm',documentroot);

However, I'm wondering if sprintf might be more obscure to use for more variables.

In the first example, should I be using a string replace for each variable, or should I use sprintf? Or am I horribly using sprintf wrong?

Any advice is greatly appreciated!

Kyle
  • 3,935
  • 2
  • 30
  • 44
  • 1
    Possible duplicate http://stackoverflow.com/questions/1386593/why-use-sprintf-function-in-php – John Giotta Dec 20 '11 at 17:11
  • You're using C++? Have you considered using stringstreams? –  Dec 20 '11 at 17:15
  • @Hurkyl This isn't really language specific; I'm doing the code in PHP, but sprintf and string replace are in several languages. – Kyle Dec 20 '11 at 17:17
  • @Kyle: I misread the question. :( –  Dec 20 '11 at 17:19
  • I think it depends on the language you are using. In C you use snprintf()/strncat(), in C++ you use std::string/std::stringstream, in Java and php you use what is best there. So pick one language. – rve Dec 20 '11 at 19:27

1 Answers1

1

Maybe you could use replace in std::string:

Please look at this examples: http://www.devx.com/getHelpOn/10MinuteSolution/16972/1954

std::string phrase = "[root]/[template_directory]/something/specific/[theme_name].htm";
std::string sought = "[root]";
std::string replacement = "newROOT";

phrase.replace(phrase.find(sought), 
               sought.size(), 
               replacement);

Good luck!

Lucian
  • 3,407
  • 2
  • 21
  • 18