2016-05-03 19 views
0

私はスカラで文字列形式のソリューションを探しています。私は以下の文字列を持っています:スカラの文字列フォーマット

str = {"card_id" : %s,"cust_id": %s,"card_info": {"card_type" : "%s","credit_limit": %s},"card_dates" : [{"date":"%s" },{"date":"%s" }]} 

ここでは、"%S"を文字列値に置き換えます。 Scalaに関数があるので、その関数を適用して適切な結果を得ることができますか?

私はstring.format("%s", str, arrayofvalue)を試しましたが、適切な結果が得られていません。

答えて

1

あなたは、単に使用することができます:私はstrとしてリテラルの中で二重引用符を使用するための"""を使用している

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:_*) 

注意を。

+0

ご返信ありがとうございます。それは正常に動作しています。 –

1

はScalaで、あなたはこの

val i = 123 
val s = " a string" 

val str = s"Here's$s $i" // result: Here's a string 123 
0

Scalaは、文字列の補間のいくつかの方法を提供して行うことができます。

S "" - 文字列補間

s"$var text ${var.func}" 

はString.Formatの

Scalaはまた、文字列

scala> "%s - %d".format("test", 9) 
res5: String = test - 9 

http://alvinalexander.com/scala/scala-string-formatting-java-string-format-method

.format方法をサポートしています http://docs.scala-lang.org/overviews/core/string-interpolation.html

を見ます

あなたがここにJSONを作成しているとして、あなたは

のような多くのJSON図書館のに使用する必要があります

libにJSONを使います

これはあなたのためにはるかに安全性を与える文字列補間よりも簡単です。

関連する問題