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

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 = ā
主站蜘蛛池模板: 霸州市| 凤翔县| 广汉市| 阆中市| 苍山县| 宽甸| 义乌市| 宣武区| 丹阳市| 麻栗坡县| 新丰县| 赤峰市| 淳化县| 沂源县| 肥乡县| 淅川县| 古浪县| 文成县| 崇明县| 南木林县| 墨竹工卡县| 阿坝县| 庄河市| 邯郸市| 同江市| 瓦房店市| 南丰县| 桦南县| 汉阴县| 同仁县| 芒康县| 武汉市| 鹿泉市| 合阳县| 万全县| 浪卡子县| 梁山县| 广西| 宣化县| 奉化市| 文水县|