Java 什么时候开始在sprintf上使用字符串替换?

Java 什么时候开始在sprintf上使用字符串替换?,java,php,c++,printf,Java,Php,C++,Printf,首先,我可能以错误的方式使用sprintf 我正在制作一个以字符串作为配置的框架插件。字符串中有一些内容需要交换,例如,一个字符串将是路径模板: [root]/[template_directory]/something/specific/[theme_name].htm 上面的例子非常具体,有很多变量需要交换 对于较少的变量,我一直这样做: sprintf('%s/some/file/path/theme.htm',documentroot); 然而,我想知道sprintf是否更难用于更多

首先,我可能以错误的方式使用sprintf

我正在制作一个以字符串作为配置的框架插件。字符串中有一些内容需要交换,例如,一个字符串将是路径模板:

[root]/[template_directory]/something/specific/[theme_name].htm
上面的例子非常具体,有很多变量需要交换

对于较少的变量,我一直这样做:

sprintf('%s/some/file/path/theme.htm',documentroot);
然而,我想知道sprintf是否更难用于更多变量

在第一个示例中,我应该为每个变量使用字符串替换,还是应该使用sprintf?还是我使用sprintf的错误太可怕了


非常感谢您的建议

也许您可以在std::string中使用replace:

请看以下示例:


祝你好运

也许您可以在std::string中使用replace:

请看以下示例:


祝你好运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);