The slice method is a method that can "slice a character string".
For example, _slice (0,5) _ cuts out the character string from the 0th () to the 5th before (that is, the 4th). () Note that, like the array, it does not start from the 1st but starts from the 0th.
def slice_1st(lyrics)
lyrics.slice(0,5) #Cut out from the 0th to the 5th before (that is, the 4th) of the given character string.
end
p slice_1st("Toy chachacha")*2 #*Output for 2 times with 2.
#=>"Toy toys"
When _slice (0..5) _ is described, the 0th to 5th character strings are cut out.
def slice_2nd(lyrics)
lyrics.slice(0..5) #Cut out from 0th to 5th of the given character string.
end
p slice_2nd("Cha-cha-cha toys")
#=>"Cha-cha-cha"
p slice_2nd("Cha-cha-cha")
#=>"Cha-cha-cha"
Of course, it is also possible to cut out the middle of the character string.
def slice_3rd(lyrics)
lyrics.slice(6..12) #Cut out the 6th to 12th of the given character string.
end
p slice_3rd("Don't Stop Me Now")
#=>"Stop Me"
Recommended Posts