gpt4 book ai didi

string - Scala 中 String.format 的缩写

转载 作者:行者123 更新时间:2023-12-04 10:05:22 25 4
gpt4 key购买 nike

Scala 中的字符串插值是否有简短的语法?就像是:

"my name is %s" < "jhonny"

代替
"my name is %s" format "jhonny"

最佳答案

不,但您可以自己添加:

scala> implicit def betterString(s:String) = new { def %(as:Any*)=s.format(as:_*) }
betterString: (s: String)java.lang.Object{def %(as: Any*): String}

scala> "%s" % "hello"
res3: String = hello

请注意,您不能使用 < ,因为这会与 Predef 中已经定义的不同的隐式转换相冲突。

关于string - Scala 中 String.format 的缩写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7716876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com