vishal vishal - 3 months ago 16
Scala Question

scala string formatting with interpolator S not working

I am new to

scala
and trying to do some string formatting.

val mAPRegisterResponseMessage = s"{\" 'type' \": \"ap_register_response\",
\"message_id\": 02,\"register_status\": $registerStatus,\"host_name_type\":
$host_name_type, \"host_name\": $host_name, \"port_num\":$port_num }"


If I remove the s in the beginning then the string literal is formed but obviously not correctly. However after adding s at the start, I get an error as unclosed character literal for the second backslash.
What am I doing wrong? Any clues will help.

Answer

You can use triple quote:

s""" {"foo" : 2, "bar": $registerStatus} """

Scala console:

scala> val registerStatus = "new"
registerStatus: String = new

scala> s""" {"foo" : 2, "bar": $registerStatus} """
res0: String = " {"foo" : 2, "bar": new} "
Comments