我正在尝试使用字符串的.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。
在scala中,对于字符串插值,我们有$,这节省了时间,让我们的生活变得更容易:
例如:你想定义一个函数,它接受输入的名字和年龄,并说Hello With名字并说它的年龄。
可以这样写:
def funcStringInterpolationDemo(name:String,age:Int)=s"Hey ! my name is $name and my age is $age"
因此,当你像这样调用这个函数:
funcStringInterpolationDemo("Shivansh",22)
它的输出将是:
Hey ! my name is Shivansh and my age is 22
您可以在同一行中编写代码来更改它,例如,如果您想在年龄上添加10年!
那么函数可以是:
def funcStringInterpolationDemo(name:String,age:Int)=s"Hey ! my name is $name and my age is ${age+10}"
现在输出是:
Hey ! my name is Shivansh and my age is 32
在scala中,对于字符串插值,我们有$,这节省了时间,让我们的生活变得更容易:
例如:你想定义一个函数,它接受输入的名字和年龄,并说Hello With名字并说它的年龄。
可以这样写:
def funcStringInterpolationDemo(name:String,age:Int)=s"Hey ! my name is $name and my age is $age"
因此,当你像这样调用这个函数:
funcStringInterpolationDemo("Shivansh",22)
它的输出将是:
Hey ! my name is Shivansh and my age is 22
您可以在同一行中编写代码来更改它,例如,如果您想在年龄上添加10年!
那么函数可以是:
def funcStringInterpolationDemo(name:String,age:Int)=s"Hey ! my name is $name and my age is ${age+10}"
现在输出是:
Hey ! my name is Shivansh and my age is 32