Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
String#delete_prefix
(::string prefix) -> ::String
delete_prefix(prefix) → new_string
string.c
View on GitHub ↗
static VALUE
rb_str_delete_prefix(VALUE str, VALUE prefix)
{
rb_len_t prefixlen;
prefixlen = deleted_prefix_length(str, prefix);
if (prefixlen <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, prefixlen, RSTRING_LEN(str) - prefixlen);
}
Returns a copy of self with leading substring prefix removed:
Example
'oof'.delete_prefix('o') # => "of" 'oof'.delete_prefix('oo') # => "f" 'oof'.delete_prefix('oof') # => "" 'oof'.delete_prefix('x') # => "oof" 'こんにちは'.delete_prefix('こん') # => "にちは"
Related: see Converting to New String.
Ruby version changes
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-