6 minutes read

In this topic, you will learn how to work with characters in Kotlin. This is a pretty fundamental concept, and being familiar with it will help you better understand how to work with texts.

What is a character

Each character is just a symbol enclosed in single quotes. The Char type represents letters (both uppercase and lowercase), digits, and other symbols:

val lowerCaseLetter: Char = 'a'
val upperCaseLetter: Char = 'Q'
val number: Char = '1'
val space: Char = ' '
val dollar: Char = '$'

This type can represent any symbol including hieroglyphs, as well as some special symbols.

A character can also be represented by its hexadecimal code in the Unicode table. The code starts with \u:

val ch = '\u0040' // it represents '@'
println(ch) // @

Although we use a sequence of characters to represent such code, the code itself represents exactly one character.

For example, Latin capital letters have hexadecimal codes from '\u0041' to '\u005A', and Latin lowercase letters have codes ranging from '\u0061' to '\u007A'.

You can also convert numbers to characters and vice versa. Let's take a look at how it works:

val ch = 'a'
println(ch.code)   // 97
val num = 97
println(num.toChar()) // a

How to read characters

Kotlin does not provide a convenient function to read a Char. However, you can do it another way: if you need to read one Char in a whole line, use this construction:

val ch: Char = readln().first()

You just read a String and get its first element, which will be a Char. Strings are composed of characters, but we will discuss this in more detail a little later.

Retrieving subsequent characters

There are two operators for adding (+) and subtracting (-) integer numbers in order to get the next and previous characters according to the Unicode order:

val ch1 = 'b'
val ch2 = ch1 + 1 // 'c'
val ch3 = ch2 - 2 // 'a'

Remember that you cannot add a symbol to a number as it will cause an error.

val ch = 'a'
val ch1 = 1 + ch // Error

You also cannot sum up two characters:

val charsSum = 'a' + 'b' // Error

It is possible to use the increment (++) and decrement (--) operators in their prefix and postfix forms. The assignment operator combined with + or - also works for characters, as well as += and -=.

var ch = 'A'

ch += 10
println(ch)   // 'K'
println(++ch) // 'L'
println(++ch) // 'M'
println(--ch) // 'L'

You cannot multiply or divide characters by numbers.

Escape sequences

There are some special characters starting with a backslash \, which are known as escape or control sequences. Most of them do not have corresponding symbols, and you cannot find them on your keyboard. To represent such characters, we use a pair of regular symbols. In a program, this pair is considered as exactly one character with the appropriate code.

  • '\n' is the newline character;

  • '\t' is the tab character;

  • '\r' is the carriage return character;

  • '\\' is the backslash character itself;

  • '\'' is the single quote mark;

  • '\"' is the double quote mark.

Here are a few examples:

print('\t') // makes a tab
print('a')  // prints 'a'
print('\n') // goes to a new line
print('c')  // prints 'c'

The above code will print:

  a
c

Note: there is also a character to represent a single space ' '. It is just a regular character, not an escape sequence.

Relational operations with characters

You can compare characters using relational operations (==, <, >, <=, >=, and !=) according to their position in the Unicode table.

println('a' < 'c')  // true
println('x' >= 'z') // false

println('D' == 'D') // true
println('Q' != 'q') // true because capital and lowercase letters are not the same

println('A' < 'a')  // true because capital Latin letters are placed before lowercase ones

Using relational operations and codes, we can check whether a Char is a digit: all ten digits have codes from '\u0030' to '\u0039'.

Here is a program that does it:

fun main() {
    val ch = readln().first()
    val isDigit = ch >= '\u0030' && ch <= '\u0039'

    println(isDigit)
}

If the input is a digit, '0', '1', '2', ..., '8' or '9' (without quotes), the program prints true. Otherwise, it prints false.

Processing characters

There's a variety of useful functions to work with characters. You can use these functions instead of dealing with character codes.

  • isDigit() returns true if the given character represents a digit ('1', '2', etc); otherwise, false;

  • isLetter() returns true if the given character represents a letter ('a', 'B', 'm', etc); otherwise, false;

  • isLetterOrDigit() returns true if the given character represents a letter or a digit; otherwise, false;

  • isWhitespace() returns true if the given character represents a whitespace (' ', or '\t', or '\n'); otherwise, false;

  • isUpperCase() returns true if the given character is an uppercase character; otherwise, false;

  • isLowerCase() returns true if the given character is a lowercase character; otherwise, false;

  • toUpperCase() returns the uppercase form of the given character (used before Kotlin 1.5; this function is now deprecated and should not be used);

  • uppercaseChar() returns the uppercase form of the given character (available since Kotlin 1.5);

  • uppercase() returns a String with the uppercase form of the given character (available since Kotlin 1.5);

  • toLowerCase() returns the lowercase form of the given character (used before Kotlin 1.5; this function is now deprecated and should not be used);

  • lowercaseChar() returns the lowercase form of the given character (available since Kotlin 1.5);

  • lowercase() returns a String with the lowercase form of the given character (available since Kotlin 1.5).

Let's take a look at some examples of the functions listed above:

val one = '1'

val isDigit = one.isDigit()   // true
val isLetter = one.isLetter() // false

val capital = 'A'
val small = 'e'

val isLetterOrDigit = capital.isLetterOrDigit() // true

val isUpperCase = capital.isUpperCase() // true
val isLowerCase = capital.isLowerCase() // false

val capitalEString = small.uppercase() // "E"
val capitalE = small.uppercaseChar()   // 'E'

There are a lot more useful functions: we will discuss them in the upcoming topics.

Conclusion

In this topic, we've learned a few things about characters. In particular, we looked at the Char type, various operators, escape sequences, and functions, and also learned what Unicode is. Now let's practice!

1122 learners liked this piece of theory. 19 didn't like it. What about you?
Report a typo