为什么IntelliJ Idea提供将字符串文字括起来以使双引号三重的原因? [英] Why does IntelliJ Idea offer to enclose a string literal to triple double quotes?
问题描述
使用IntelliJ Idea编写Scala时,我注意到它用于将字符串文字括在第二对双引号中. «转换为""»-它说.如果我同意-它只会在字符串之前再加上2个双引号,在字符串之后再加上2个,使我的字符串看起来像
While using IntelliJ Idea to code Scala I've noticed it to use to offer to enclose a string literal in a second pair of double quotes. «Convert to """string"""» - it says. If I agree - it just adds a 2 more double quote marks before and 2 more after the string, having my string to look like
val myString = """my string value"""
这有什么意义?三重双引号在Scala中意味着特殊之处吗?
How does this make any sense? Do triple double quotes mean something special in Scala?
推荐答案
With IntelliJ smart String, there should be only two cases.
- 简单字符串(您可以将其转换为三重双引号多行String文字)
- 多行String文字,早在 Scala 2.1.7 (您可以将其转换回单个字符串)
- simple string (which you can convert to triple double-quotes multi-line String literal)
- multi-line String literal, introduced way back in Scala 2.1.7 (which you can convert back to a single String)
因此,当您看到此内容时:
So when you see this:
您应该以"""
结尾.如果不是,则应将其报告为 youtrack Scala项目中的错误.
you should end up with """
. If not, this should be reported as a bug in the youtrack Scala project.
但是如果有三重双引号,则IntelliJ可以将您的简单String正确地转换为多行String文字,如
But if there were triple double quotes, then IntelliJ correctly transformed your simple String into a multi-line String literal, which, as illustrated by langref, allows you to define:
val text = "This\nIs\nA\nMultiline\nString"
进入
val text = """This
Is
A
Multiline
String"""
Rosetta代码提及行为在Scala 2.7和2.8之间发生了变化:>
Rosetta code mentions the behavior has changed between Scala 2.7 and 2.8:
在2.7版中,三重双引号字符串在第三个连续的引号处结束,在2.8版中,它以一系列至少三个双引号的最后一个引号结束.
on version 2.7, the triple-double-quoted string ends at the third consecutive quote, on version 2.8 it ends on the last quote of a series of at least three double-quotes.
Scala 2.7:
Scala 2.7:
scala> val error = """can't finish with a quote: """"
<console>:1: error: unterminated string
val error = """can't finish with a quote: """"
^
Scala 2.8
Scala 2.8
scala> val success = """but it can on 2.8: """"
success: java.lang.String = but it can on 2.8: "
这篇关于为什么IntelliJ Idea提供将字符串文字括起来以使双引号三重的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!