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

  • 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 = ā
主站蜘蛛池模板: 府谷县| 岢岚县| 湘潭县| 双柏县| 赤壁市| 沙雅县| 积石山| 宁武县| 敦化市| 开江县| 舟曲县| 平昌县| 大庆市| 临沭县| 会昌县| 万安县| 宁国市| 湘阴县| 安新县| 永清县| 宜昌市| 凉城县| 监利县| 绥阳县| 特克斯县| 鄂托克前旗| 客服| 凤庆县| 宿迁市| 百色市| 宜丰县| 麟游县| 丹棱县| 全南县| 盐源县| 荆州市| 北京市| 治县。| 宁强县| 象州县| 康保县|