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

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.

主站蜘蛛池模板: 芦溪县| 大安市| 安福县| 汾阳市| 阿拉善盟| 商都县| 临澧县| 宝山区| 松溪县| 凤城市| 西充县| 三明市| 罗平县| 灵武市| 扶绥县| 安化县| 新蔡县| 广宁县| 蓝山县| 桦南县| 土默特左旗| 长白| 千阳县| 西和县| 广西| 文水县| 南靖县| 那曲县| 阜城县| 大余县| 图木舒克市| 金阳县| 广平县| 阳山县| 开封县| 醴陵市| 康定县| 容城县| 湖口县| 隆昌县| 兴安县|