Crystal has a type known as String
, which is used to represent text.
Crystal strings are sequences of Unicode characters.
Strings are immutable, meaning they cannot be changed once created.
This means that a new string is created every time you modify a string; therefore, the original string is not modified.
To define a string you use double quotes "
.
"Hello World"
To concatenate two strings, use the +
operator.
The +
operator will return a new string combining the two strings.
hello = "Hello"
hello + " World"
# => "Hello World"
The to_i
method converts a string to an integer.
Alternatively, the to_f
method can be used to return a floating point number.
If the string can't be converted to a number then an ArgumentError
will be raised.
"1".to_i
# => 1
"1.0".to_f
# => 1.0
"Hello".to_i
# => ArgumentError: Invalid integer: Hello
An integer or floating point number can be converted to a string using the to_s
method.
1.to_s
# => "1"
1.0.to_s
# => "1.0"
Interpolation is a convenient way to combine strings and embed expressions in strings.
To interpolate a string, you can use the #
character followed by curly braces {}
with the expression inside the braces.
name = "World"
"Hello #{name}!"
# => "Hello World!"
Crystal will automatically convert the result of the expression to a string.
"Hello #{1 + 1}!"
# => "Hello 2!"
When you need to know the number of characters in a string, you can use the size
method, which returns the length of the string as an integer.
The size of a string is a stored property of the string, so it doesn't have to calculate the size every time you call the method, making it very fast.
"Hello World".size
# => 11
Indexing is when you want to get a specific character from a string.
To get a character from a String
you can use familiar bracket notation.
[]
is implemented as a String instance method, where the index is the method argument.
Crystal is the first character in a string at index 0.
"Hello World"[0]
# => 'H'
"Hello World"[6]
# => 'W'
To get a character from the end of a string, you can take the length of the string minus one to get the index of the last character, and to get the second last character, you can take the length of the string minus two, and so on. There is a shortcut for this and that is to simply use a negative index, where the last character is at index -1, the second last character is at index -2, and so on.
name = "Hello World"
name[name.size - 1]
# => 'd'
name[-1]
# => 'd'
Crystal supports testing if a string contains a substring using indexing.
The same brackets notation does it, but instead of an index, a substring is given.
If the substring is found in the string then the substring is returned, otherwise a NilAssertionError
is raised.
"Hello World"["Hello"]
# => "Hello"
"Hello World"["Goodbye"]
# => NilAssertionError
If you want a Bool
value to indicate if the substring is found, then you can use the includes?
method.
"Hello World".includes?("Hello")
# => true
"Hello World".includes?("Goodbye")
# => false
Some characters can't be written in a string directly, instead, you have to use an escape sequence.
For example, if you want to use double quotes in a string, you have to escape the double quotes.
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 |
puts "Hello \"World\""
# => Hello "World"
puts "Hello \nWorld"
# => Hello
# => World
To write a multi-line string, you use the same syntax as a single-line string but make a new line for every line you want in the string.
"Hello
World"
# => "Hello\nWorld"
Unicode is a standard for encoding, representing, and processing text in different writing systems. Unicode supports over 100,000 characters, including letters, punctuation, symbols, and emoji.
Since Crystal uses Unicode for strings, you can, for example, put emojis in strings.
"Hello 🌍"
# => "Hello 🌍"
Sometimes, you want to use a character that is not available in, for example, your editor or system. In that case, you can use the Unicode escape sequence to write the character.
"\u{1F310}"
# => "🌐"