Tracks
/
Crystal
Crystal
/
Exercises
/
High School Sweetheart
High School Sweetheart

High School Sweetheart

Learning Exercise

Introduction

String has several methods that can be used to manipulate strings.   These methods are called on the string itself and are often chained together to perform multiple operations on a string.

The following is a list of some of the most common methods. See the String API documentation for a complete list of methods.

String#upcase & String#downcase

The upcase method returns a new string with all the characters converted to uppercase.

"hello".upcase
# => "HELLO"

The downcase method returns a new string with all the characters converted to lowercase.

"HELLO".downcase
# => "hello"

String#sub & String#gsub

Crystal has methods for replacing parts of a string. These are known as sub (substitution) and gsub (global substitution).

These methods can take a string, char, or regex as the first argument and a string or char as the second argument. The first argument is the part of the string that will be replaced, and the second argument is the replacement. Char and regex will be explained in later concepts.

The sub method will replace the first occurrence of the first argument with the second argument.

"hello".sub("l", "r")
# => "herlo"

The gsub method works the same as sub but will replace all occurrences of the first argument with the second argument.

"hello".gsub("l", "r")
# => "herro"

String#chomp & String#strip

Chomp and strip can be used to remove unwanted characters from a string.

The chomp method will be default remove the last \r, \n or \r\n characters from a string. If the method is called with an argument, it will remove the given value from the end of the string. If it does exist at the end of the string.

"hello\n".chomp
# => "hello"

"hello".chomp("llo")
# => "he"

By default, the strip method will remove all whitespace from the beginning and end of a string. If the method is called with an argument, it will remove that argument from the beginning and end of the string. If it does exist at the beginning or end of the string.

" hello ".strip
# => "hello"

String#index

The index method can be used to find a character's index in a string. The index method takes a String, Char, or regex as an argument. It can take an optional second argument to specify the index from which to search. The index method will return the index of the first occurrence of the argument.

"hello".index("l")
# => 2

"hello".index("l", 3)
# => 3

String#delete_at

You can use the delete_at method to remove a character from a string. The delete_at method takes an index as an argument and will remove the character at that index.

"hello".delete_at(0)
# => "ello"

"hello".delete_at(3)
# => "helo"

String#insert

The insert method can be used to insert a string at a specific index. The insert method takes an index as the first argument and a string as the second argument. The insert method will return a new string with the second argument inserted at the index.

"hello".insert(0, "h")
# => "hhello"

"hello".insert(3, "l")
# => "helllo"

Instructions

In this exercise, you are going to help high school sweethearts profess their love on social media by generating an ASCII heart with their initial:

❤ J.  +  M. ❤

1. Clean up the name

Implement the HighSchoolSweetheart.clean_up_name method. It should take a name and remove all - characters from it and replace them with a space. It should also remove any whitespace from the beginning and end of the name.

HighSchoolSweetheart.clean_up_name("Jane-Ann")
# => "Jane Ann"

2. Get the name's first letter

Implement the HighSchoolSweetheart.first_letter method. It should take a name and return its first letter. Make sure to reuse HighSchoolSweetheart.clean_up_name that you defined in the previous step.

HighSchoolSweetheart.first_letter("Jane")
# => "J"

3. Format the first letter as an initial

Implement the HighSchoolSweetheart.initial method. It should take a name and return its first letter, uppercase, followed by a dot. Make sure to reuse HighSchoolSweetheart.first_letter that you defined in the previous step.

HighSchoolSweetheart.initial("Robert")
# => "R."

4. Put the initials inside of the heart

Implement the HighSchoolSweetheart.pair method. It should take two names and return the initials with emoji hearts around. Make sure to reuse HighSchoolSweetheart.initial that you defined in the previous step.

HighSchoolSweetheart.pair("Blake Miller", "Riley Lewis")
# => "❤ B.  +  R. ❤"
Edit via GitHub The link opens in a new window or tab
Crystal Exercism

Ready to start High School Sweetheart?

Sign up to Exercism to learn and master Crystal with 26 concepts142 exercises, and real human mentoring, all for free.