我正在尝试使用字符串的 .format
方法。但是,如果我在字符串中放置 %1、%2 等,则会抛出 java.util.UnknownFormatConversionException,指向一个令人困惑的 Java 源代码片段:
private void checkText(String s) {
int idx;
// If there are any '%' in the given string, we got a bad format
// specifier.
if ((idx = s.indexOf('%')) != -1) {
char c = (idx > s.length() - 2 ? '%' : s.charAt(idx + 1));
throw new UnknownFormatConversionException(String.valueOf(c));
}
}
据我所知, %
字符是被禁止的。如果是这样,那么我应该为参数占位符使用什么?
我使用 Scala 2.8.
最佳答案
虽然前面的所有响应都是正确的,但它们都是 Java 语言。这是一个 Scala 示例:
val placeholder = "Hello %s, isn't %s cool?"
val formatted = placeholder.format("Ivan", "Scala")
我还有一篇关于 making format
like Python's %
operator 的博文这可能有用。
关于java - 如何在 Scala 中使用 java.String.format?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3695230/