什么时候应该开始使用字符串替换而不是 sprintf?
首先,我可能以错误的方式使用 sprintf 。
我正在制作一个以字符串作为配置的框架插件。字符串中有需要交换的内容,例如,一个字符串将是一个路径模板:
[root]/[template_directory]/something/specific/[theme_name].htm
上面的示例非常具体,并且有很多变量需要交换。
对于较少的变量,我一直这样做:
sprintf('%s/some/file/path/theme.htm',documentroot);
但是,我想知道 sprintf 对于更多的变量是否会更加晦涩。
在第一个示例中,我应该对每个变量使用字符串替换,还是应该使用 sprintf?或者我使用 sprintf 是错误的吗?
非常感谢任何建议!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你可以在 std::string 中使用替换:
请看这个例子:
http://www.devx.com/getHelpOn/10MinuteSolution/16972/1954
祝你好运!
Maybe you could use replace in std::string:
Please look at this examples:
http://www.devx.com/getHelpOn/10MinuteSolution/16972/1954
Good luck!