vishal vishal - 1 year ago 125
Scala Question

scala string formatting with interpolator S not working

I am new to

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 Source

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} "
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download