官术网_书友最值得收藏!

String literals

We've already used String literals in several places at this point. So here, apart from a formal introduction to what a String literal is, we'll take a look at how String literals in Scala are different, since there's more than one way to write String literals. Up till now we've declared String literals within double quotes:

scala> val boringString = "I am a String Literal."
boringString: String = I am a String Literal.

So let's start with a String literal declaration within triple quotes. Sounds interesting! Isn't it? Take a look at this:

scala> val interestingString = """I am an Interesting String
| Also span in multiple Lines!
| Ok, That's it about me"""
interestingString: String =
"I am an Interesting String
Also span in multiple Lines!
Ok, That's it about me"

Got an idea about it after seeing it? A string quoted in triple quotes can be spanned over multiple lines, thus they are called multi-line string literals. These are also called raw strings, because if you try to give any escape character inside triple quotes, these multi-line string literals treat them as normal characters:

scala> val aString = """ / " ' """
aString: String = " / " ' "
scala> println(aString)

/ " '

So these escape characters are treated as defined if inside multi-line strings. This can contain any character, even spaces. There's more to strings, such as string interpolation, where we can dynamically assign values to a string from the current scope. For that, we use interpolators. We'll study these further a little later on in this chapter.

主站蜘蛛池模板: 成都市| 昌平区| 西和县| 抚远县| 安塞县| 浙江省| 龙山县| 宝兴县| 伊宁市| 区。| 广平县| 乌鲁木齐市| 长兴县| 葫芦岛市| 青冈县| 关岭| 银川市| 凤山县| 丰台区| 循化| 文安县| 石景山区| 萨嘎县| 长葛市| 保定市| 德阳市| 休宁县| 南木林县| 侯马市| 冷水江市| 泾源县| 左权县| 金溪县| 孟津县| 遵化市| 阳山县| 太和县| 大英县| 五家渠市| 搜索| 乾安县|