- Learning Scala Programming
- Vikash Sharma
- 267字
- 2021-06-30 19:07:49
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.
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- Flask Web全棧開發實戰
- Learning RxJava
- 算法大爆炸:面試通關步步為營
- Learning Selenium Testing Tools(Third Edition)
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- 基于Struts、Hibernate、Spring架構的Web應用開發
- Swift 4 Protocol-Oriented Programming(Third Edition)
- 算法圖解
- 交互式程序設計(第2版)
- C++服務器開發精髓
- Java EE 7 Development with WildFly
- HTML5/CSS3/JavaScript技術大全
- JavaScript全棧開發
- JavaScript程序設計實例教程(第2版)