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'
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"
Once there was an eccentric programmer living in a strange house with barred windows. One day he accepted a job from an online job board to build a party robot. The robot is supposed to greet people and help them to their seats. The first edition was very technical and showed the programmer's lack of human interaction. Some of which also made it into the next edition.
Implement the welcome
method to return a welcome message using the given name:
PartyRobot.welcome("Christiane")
# => Welcome to my party, Christiane!
Implement the happy_birthday
method to return a birthday message using the given name and age of the person.
Unfortunately the programmer is a bit of a show-off, so the robot has to demonstrate its knowledge of every guest's birthday.
PartyRobot.happy_birthday("Frank", 58)
# => Happy birthday Frank! You are now 58 years old!
Implement the assign_table
function to give directions.
It should accept 5 parameters.
String
)Int
)String
)String
)Float64
)The exact result format can be seen in the example below.
The seatmate's should be shorted to the first and last letter of the name. Frank becomes Fk, Christian becomes Cn, and so on.
The robot provides the table number. The robot also mentions the distance of the table. Fortunately only with a precision that's limited to 1 digit.
PartyRobot.assign_table("Christiane", "on the left", 27, 23.7834298, "Frank")
# =>
# Welcome to my party, Christiane!
# You have been assigned to table 27. Your table is on the left, exactly 23.8 meters from here.
# You will be sitting next to Fk.
Sign up to Exercism to learn and master Crystal with 26 concepts, 133 exercises, and real human mentoring, all for free.