2011-12-20 92 views
1

首先,我可能会以错误的方式使用sprintf。什么时候应该开始使用字符串替换sprintf?

我正在制作一个框架插件,它将字符串作为配置。该字符串有事情需要被换出,例如,一个字符串将是一个路径模板:

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

上面的例子是非常具体的,有很多的变量被换出。

对于不变量,我已经做了,像这样:

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

不过,我想知道如果sprintf的可能是比较模糊的使用更多的变数。

在第一个例子中,我应该使用一个字符串替换每个变量,还是应该使用sprintf?或者我可怕地使用sprintf错误?

任何意见非常感谢!

+1

可能的重复http://stackoverflow.com/questions/1386593/why-use-sprintf-function-in-php – 2011-12-20 17:11:36

+0

你正在使用C++?你有没有考虑过使用stringstreams? – Hurkyl 2011-12-20 17:15:00

+0

@Hurkyl这不是特定语言;我在PHP中执行代码,但sprintf和字符串替换有几种语言。 – Kyle 2011-12-20 17:17:36

回答

1

也许你可以使用的std :: string取代:

请看看这个例子: 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); 

祝你好运!

相关问题