Ch

Char in Crystal

1 exercise

About Char

Char is a data type in Crystal representing a 32-bit Unicode code point. Their size makes them memory efficient and gives them specific properties that Strings doesn't have. They are created using single quotes (') and only allow one Unicode code point to be placed inside.

'a' # => 'a'
'a'.class # => Char

It is quite common to work with Chars when working with Strings in Crystal since quite a few methods in the String class return Chars or take Chars as arguments. For example, the String#[] method returns a Char, or the String#chars method returns a collection of Chars.

Incrementing / Decrementing

Due to char being a 32 Unicode point do they support incrementing and decrementing, this makes it so if you have 'a' so can you get 'b' by adding one.

'a' + 5
# => 'f'

'z' - 25
# => 'a'

There is also a method called Char#succ, which returns the next char in the Unicode table, and Char#pred, which returns the previous char in the Unicode table.

'a'.succ
# => 'b'

'z'.pred
# => 'y'

concatenation

Since Chars is only meant to represent a 32-bit Unicode, two chars can't be concatenated. Therefore, Chars can only be concatenated with Strings.

'a' + "bc"
# => "abc"

'a' + 'b'
# => error: expected argument #1 to 'Char#+' to be Int or String, not Char

Properties methods

Crystal has a few methods for getting information about a Character, such as whether it is a number or a letter or whether it is uppercase or lowercase. Here is a list of some of the methods, for a complete list see the Char API:

Method Description
Char#number? Returns true if the Char is a number
Char#letter? Returns true if the Char is a letter
Char#lowercase? Returns true if the Char is lowercase
Char#uppercase? Returns true if the Char is uppercase
Char#ascii? Returns true if the Char is an ASCII character
Char#whitespace? Returns true if the Char is a whitespace character

Converting

Converting between code points and Chars

Since Chars is a 32-bit Unicode point, can you convert between Chars and code points (Int). This can be done using the Char#ord method which will return the code point of the Char, or the Int#chr method which will return the Char of the code point.

'a'.ord
# => 97

97.chr
# => 'a'

Converting between types

As with other data types, chars have a few methods for converting between types. The most common one is the Char#to_s method, which converts the Char to a String. However, there is also the Char#to_i method, which converts the Char to an Int, and the Char#to_f method, which converts the Char to a Float.

'a'.to_s
# => "a"

'4'.to_i
# => 4

'a'.to_i
# => Error: Invalid integer: a (ArgumentError)

Escaping

As with Strings, there are certain characters that can't be written directly, and to write them, you need to escape them. You can use the \ character to write an escape sequence followed by the character you want to escape.

These are the special characters in Crystal:

Value Description
\a Alert
\b Backspace
\e Escape
\f Form feed
\n Line feed or newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quote
\" Double quote
\377 Octal ASCII character
\xFF Hexadecimal ASCII character
\uFFFF Hexadecimal unicode character
Edit via GitHub The link opens in a new window or tab

Learn Char

Practicing is locked

Unlock 11 more exercises to practice Char