- Learning Scala Programming
- Vikash Sharma
- 205字
- 2021-06-30 19:07:49
Character literals
What if you want to break down some words and spaces into separate characters? If you do so, you're creating Character literals. We represent Character literals in single quotes. Any Unicode character or escape sequence can be represented as a Character literal. What's an escape sequence, by the way? Let's take this backslash for example. If we try this:
scala> val aChar = '\'
<console>:1: error: unclosed character literal
val aChar = '\'
This will not work at all, because this '\' is an escape character. By definition, an escape sequence or character is something that does not represent itself in String or Character literal. To define these characters, we use this sequence:
scala> val doublequotes = "\""
doublequotes: String = "
scala> val aString = doublequotes + "treatme a string" + doublequotes
aString: String = "treatme a string"
In the preceding code, we used our doublequotes as prefix and suffix to our string treatme a string, and get a response.
We've a list of escape sequence characters shown in the following table:

You can also use the hex code to represent a Character literal, but we need to put a \u preceding it:
scala> val c = '\u0101'
c: Char = ā
- Spring 5.0 Microservices(Second Edition)
- 零起步玩轉掌控板與Mind+
- Objective-C應用開發全程實錄
- Python爬蟲開發:從入門到實戰(微課版)
- Cross-platform Desktop Application Development:Electron,Node,NW.js,and React
- 機器人Python青少年編程開發實例
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- 實戰Java高并發程序設計(第3版)
- Learning FuelPHP for Effective PHP Development
- Flutter跨平臺開發入門與實戰
- 打開Go語言之門:入門、實戰與進階
- Django 3.0入門與實踐
- Bootstrap for Rails
- Android技術內幕(系統卷)
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐