As we go through our programming careers, we'll be working a lot with text. To facilitate this, we'll want to get familiar with some common string methods, and one great way to do this is by implementing them ourselves!
For this exercise, write an implementation of the following standard string methods.
Find the length of the given string:
length("pizza")
=> 5
Capitalize all of the letters in a string:
upcase("pizza")
=> PIZZA
Take a string, a substring to replace, and a string with which to replace it. Return a new string that has the first appearance of the substring-to-replace swapped for the replacement string:
sub("dog", "d", "f")
=> "fog"
sub("dud", "d", "f")
=> "fud"
Similar to sub
, but replace all occurrences of the replacement string:
gsub("dud", "d", "f")
=> "fuf"
Take a string and return an array of strings which are "split" around any whitespace characters.
split("Hello Dear Friends")
=> ["Hello", "Dear", "Friends"]
Additionally, allow the user to provide an argument specifying around which character the string should be split:
split_with_arg("one,two,three", ",")
=> ["one", "two", "three"]
Take a string, a start index, and an end index, and return the "substring" starting from the first position and ending at the last:
(remember that in programming we generally count starting from 0)
substring("pizza", 3, 4)
=> "za"
substring("pizza", 1,4)
=> "izza"