String 多行字符串文字仅在REPL和工作表中表现正常

String 多行字符串文字仅在REPL和工作表中表现正常,string,scala,line-breaks,multiline,string-literals,String,Scala,Line Breaks,Multiline,String Literals,答复: 工作表: scala> val a = "hello\nworld" a: String = hello world scala> val b = """hello | world""" b: String = hello world scala> a == b res0: Boolean = true 普通Scala代码: val a = "hello\nworld" //> a : Strin

答复:

工作表:

scala> val a = "hello\nworld"
a: String = 
hello
world

scala> val b = """hello
     | world"""
b: String = 
hello
world

scala> a == b
res0: Boolean = true
普通Scala代码:

val a = "hello\nworld"                        //> a  : String = hello
                                              //| world

val b = """hello
world"""                                      //> b  : String = hello
                                              //| world

a == b                                        //> res0: Boolean = true
输出:

val a = "hello\nworld"

val b = """hello
world"""

println(a)
println(b)
println(a == b)
println(a.map(_.toInt))
println(b.map(_.toInt))
为什么在REPL和工作表中的比较结果为true,而在普通Scala代码中为false


有趣的是,
b
似乎比
a
长一个字符,所以我打印了Unicode值:

hello
world
hello
world
false
输出:

val a = "hello\nworld"

val b = """hello
world"""

println(a)
println(b)
println(a == b)
println(a.map(_.toInt))
println(b.map(_.toInt))

这是否意味着多行字符串文字具有与平台相关的值?我在Windows上使用Eclipse。

我想这是因为

尝试检查
a.toList.length
b.toList.length
。似乎
b==“你好\r\nworld”


多行字符串文字值不取决于平台,而是取决于源文件的编码。实际上,您将在
“”“
。如果有
\r\n
您将在
字符串中找到它

您的IDE文档中必须有一个空白。你检查过两份报告的长度都是11吗?如果存在差异,您也可以使用
a.getBytes
进行检查。本周第二次。