本文介绍了Scala 中的字符串格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 Scala 中寻找字符串格式解决方案.我有以下字符串:
I am looking for a string format solution in Scala. I have the below string:
str = {"card_id" : %s,"cust_id": %s,"card_info": {"card_type" : "%s","credit_limit": %s},"card_dates" : [{"date":"%s" },{"date":"%s" }]}
这里我想用字符串值替换 "%S"
.Scala 中是否有任何函数可以应用该函数并获得正确的结果?
And here I want to replace "%S"
with string value. Is there any function in Scala so that I can apply that function and get the proper result?
我已经尝试过 string.format("%s", str, arrayofvalue)
但它没有给出正确的结果.
I have tried string.format("%s", str, arrayofvalue)
but it's not giving the proper result.
推荐答案
你可以简单地使用:
val str = """{"card_id" : %s,"cust_id": %s,"card_info": {"card_type" : "%s","credit_limit": %s},"card_dates" : [{"date":"%s" },{"date":"%s" }]}"""
str.format(arrayofvalue:_*)
请注意,我使用 """
在 str
中使用双引号作为文字.
Note that I have use """
for using double quote in str
as literal.
这篇关于Scala 中的字符串格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!