Ruby Strings In Depth
Strings are mutable. Useful methods: length, upcase, downcase, capitalize, reverse, include?, start_with?, sub, gsub, strip, center, ljust, rjust and slicing with [].
Example
s = "hello ruby"
puts s.capitalize, s.length, s[0, 5], s[-4..]
puts s.sub("ruby", "world")
puts s.center(15, "*")
puts s.split.map(&:capitalize).join(" ")Result
Hello ruby 10 hello ruby hello world **hello ruby*** Hello Ruby
Exercise
Replace all matches.
s.("a", "b")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.