Getting the Parts of a String You Want : String Range « String « Ruby






Getting the Parts of a String You Want


s = 'My kingdom for a string!'
s.slice(3,7)                        # => "kingdom"
s[3,7]                              # => "kingdom"
s[0,3]                              # => "My "
s[11, 5]                            # => "for a"
s[11, 17]                           # => "for a string!"

 








Related examples in the same category

1.enter a string as the argument to []=, and it will return the new, corrected string, if found; nil otherwise.
2.enter a range to indicate a range of characters you want to change. Include the last character with two dots (..)
3.Get substring from a string object
4.String range with string.length
5.Generating a Succession of Strings
6.Change a string with range