带有插值器 S 的 Scala 字符串格式不起作用 [英] scala string formatting with interpolator S not working
本文介绍了带有插值器 S 的 Scala 字符串格式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 scala
的新手,正在尝试进行一些字符串格式化.
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 }"
如果我删除开头的 s,则形成字符串文字,但显然不正确.但是,在开始添加 s 后,第二个反斜杠出现 unclosed character literal 错误.我究竟做错了什么?任何线索都会有所帮助.
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.
推荐答案
可以使用三引号:
s""" {"foo" : 2, "bar": $registerStatus} """
Scala 控制台:
scala> val registerStatus = "new"
registerStatus: String = new
scala> s""" {"foo" : 2, "bar": $registerStatus} """
res0: String = " {"foo" : 2, "bar": new} "
这篇关于带有插值器 S 的 Scala 字符串格式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文