如何在 Scala 中使用 java.String.format?
我正在尝试使用字符串的 .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));
}
}
由此我了解到 %
char 是被禁止的。如果是这样,那么我应该使用什么作为参数占位符?
我使用 Scala 2.8。
I am trying to use a .format
method of a string. But if I place %1, %2, etc. in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece:
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));
}
}
From this I understand that %
char is forbidden. If so, then what should I use for argument placeholders?
I use Scala 2.8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
虽然之前的所有回答都是正确的,但它们都是用 Java 编写的。这是一个 Scala 示例:
我还有一篇关于 制作
format
就像 Python 的%
运算符 可能有用。While all the previous responses are correct, they're all in Java. Here's a Scala example:
I also have a blog post about making
format
like Python's%
operator that might be useful.您不需要使用数字来指示位置。默认情况下,参数的位置就是它在字符串中出现的顺序。
下面是正确使用方法的示例:
您将始终使用
%
后跟一些其他字符,让该方法知道应该如何显示字符串。%s
可能是最常见的,它只是意味着参数应该被视为字符串。我不会列出每个选项,但我会举几个例子,只是为了给您一个想法:
String.format
仅使用java.util.Formatter
,所以有关选项的完整说明,您可以查看 格式化程序 javadocs。而且,正如 BalusC 提到的,您将在文档中看到,如果需要,可以更改默认参数顺序。但是,可能您唯一需要/想要执行此操作的情况是多次使用相同的参数。
You don't need to use numbers to indicate positioning. By default, the position of the argument is simply the order in which it appears in the string.
Here's an example of the proper way to use this:
You will always use a
%
followed by some other characters to let the method know how it should display the string.%s
is probably the most common, and it just means that the argument should be treated as a string.I won't list every option, but I'll give a few examples just to give you an idea:
String.format
just uses ajava.util.Formatter
, so for a full description of the options you can see the Formatter javadocs.And, as BalusC mentions, you will see in the documentation that is possible to change the default argument ordering if you need to. However, probably the only time you'd need / want to do this is if you are using the same argument more than once.
您应该阅读 javadoc String.format() 和 格式化程序语法。
您可以指定 % 之后的值的格式。例如,对于十进制整数,它是
d
,对于字符串,它是s
:输出:
要执行您尝试的操作(使用参数索引),请使用:
*n*$
,输出:
Instead of looking at the source code, you should read the javadoc String.format() and Formatter syntax.
You specify the format of the value after the %. For instance for decimal integer it is
d
, and for String it iss
:Output:
To do what you tried (use an argument index), you use:
*n*$
,Output:
你可以使用这个;
输出:
You can use this;
Output:
另请注意,Scala 使用多种方法扩展了 String(通过隐式转换为 Predef 引入的 WrappedString),因此您还可以执行以下操作:
Also note that Scala extends String with a number of methods (via implicit conversion to a WrappedString brought in by Predef) so you could also do the following:
官方参考是类
Formatter
。
The official reference is the class
Formatter
.在 Scala 2.10 中
In Scala 2.10
这是
String.format
可以执行的操作的列表。printf
也是如此This is a list of what
String.format
can do. The same goes forprintf
以下是与 String.format() 一起使用的格式化程序列表
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
Here is a list of formatters used with String.format()
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
虽然@Londo提到了Scala的“s”字符串插值器,但我认为Scala的 “f”字符串插值器与原始问题更相关。在其他响应中使用过几次的示例也可以这样编写(自 Scala 2.10 起):
与原始问题的联系是要注意:
formatted
是用前缀为的字符串定义的字母“f”。这是“f”(格式化)字符串插值器。java.util.Formatter
java.lang.String.format
使用相同的java.util.Formatter
关于字符串插值的一点是,它可以让您查看哪个变量被直接替换到字符串中,而不必将其与 String.format 方法的参数进行匹配。
Although @Londo mentioned Scala's "s" string interpolator, I think Scala's "f" string interpolator is more relevant to the original question. The example used a few time in other responses could also be written (since Scala 2.10) this way:
The connection to the original question is to be aware that:
formatted
is defined with a string that is prefixed with the letter "f". This is the "f" (formatting) string interpolator.java.util.Formatter
java.lang.String.format
uses the samejava.util.Formatter
The nice thing about string interpolation is that it lets you see which variable is being substituted directly into the string instead of having to match it with the arguments to the
String.format
method.在 scala 中,对于字符串插值,我们有 $ 来拯救这一天并使我们的生活变得更加轻松:
例如: 您想要定义一个函数,它接受输入的姓名和年龄以及用名字说“你好”并说出它的年龄。
可以这样写:
因此,当您调用此函数时: 像这样:
它的输出将是:
您可以在同一行中编写代码来更改它,就像您想在年龄上添加 10 年一样!
那么函数可以是:
现在输出将是:
In scala , for string Interpolation we have $ that saves the day and make our life much easy:
For Example: You want to define a function that takes input name and age and says Hello With the name and says its age.
That can be written like this:
Hence , When you call this function: like this :
Its output would be :
You can write the code to change it in the same line, like if you want to add 10 years to the age !
then function could be :
And now the output would be :