Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道在java中我们可以做类似的事情:
String s = "I am a %s %s" s = s.format("format", "string")
我想知道是否可以一次只替换一个%s的出现,此外,我是否可以给这些命名,例如%1, %2。我想要实现的是拥有一个巨大的 html 字符串,其中每个方法都将完成该字符串的特定部分。该字符串将以迭代方式完成,我想确保每个方法都完成自己的部分。
%s
%1
%2
从技术上讲,您可以简单地使用String.replace:
String.replace
s = s.replace("%1", value1); s = s.replace("%2", value2);
根据您想要做的事情,您可能需要也可能不需要考虑安全性以及使用它在您的 html 中注入恶意脚本的人。在后一种情况下,清理用户输入是重中之重。