什么时候应该开始使用字符串替换而不是 sprintf?

发布于 2024-12-22 15:32:07 字数 441 浏览 4 评论 0原文

首先,我可能以错误的方式使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-12-29 15:32:07

也许你可以在 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);

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文