Читать книгу Programming Kotlin Applications - Бретт Мак-Лахлин, Brett McLaughlin - Страница 59

Letters and Things

Оглавление

If you want to represent a single character—including special characters like backslash or a carriage return—you use the Char type:

val joker = 'j'

A character should be enclosed in a set of single quotes. You can also enclose special characters in quotes and precede the character code with a backslash. For instance, tab is \t, a line feed is \n, and a backslash is itself \\ :

val special = '\n'

NOTE Backslash is weird because it is itself an escape character. To get an actual backslash, you use the escape character ( \ ) and then another backslash ( \ ), which gets you \\ .

For sequences of characters, you likely want a String. You can create a string with text enclosed in double quotes:

val letters = "abcde"

Simple enough! But note that a single letter in double quotes is a String, while a single letter in single quotes is a Char :

val letter = "a" val notStringy = 'a'

Programming Kotlin Applications

Подняться наверх