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

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 = ā
主站蜘蛛池模板: 长宁县| 清镇市| 留坝县| 穆棱市| 特克斯县| 中西区| 宜昌市| 临潭县| 老河口市| 湾仔区| 集安市| 蕉岭县| 兴文县| 邵阳县| 大竹县| 怀化市| 乌兰浩特市| 广安市| 犍为县| 四会市| 高州市| 如东县| 辽阳县| 永清县| 喜德县| 枝江市| 邵武市| 东山县| 武强县| 民勤县| 镇平县| 临西县| 吉隆县| 上蔡县| 大城县| 莱芜市| 邢台县| 武冈市| 宜春市| 合山市| 托克托县|