Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

String#slice!

str.slice!(integer) → new_str or nil
str.slice!(integer, integer) → new_str or nil
str.slice!(range) → new_str or nil
str.slice!(regexp) → new_str or nil
str.slice!(other_str) → new_str or nil

Deletes the specified portion from str, and returns the portion deleted.

Example
string = "this is a string"
string.slice!(2)        #=> "i"
string.slice!(3..6)     #=> " is "
string.slice!(/s.*t/)   #=> "sa st"
string.slice!("r")      #=> "r"
string                  #=> "thing"

Ruby version changes

  1. 3.0
See also #slice #split #size #squeeze also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1