【发布时间】:2021-03-26 12:49:48
【问题描述】:
在 Kotlin 中给定一个字符串,我如何以字符串文字格式打印它?
或者以更长的形式,给定一些名为 foo 的方法,它会执行以下操作:
println("Howdy".foo()) --> "Howdy" (quotes included in the output)
println("1 line\n\tand a tab".foo()) --> "1 line\n\tand a tab"
println("\"embeded quotes\"".foo()) --> "\"embeded quotes\""
基本上,我正在尝试创建与字符串的代码形式匹配的调试输出。 toString 只返回字符串,而不是在代码中重新创建它的修饰/转义。
【问题讨论】:
-
如果您使用 Kotlin JVM,请查看 Java version of this question,由于 Kotlin-Java 互操作,您应该能够在 Kotlin JVM 中轻松使用这些解决方案