Maintenance Ruby 3.4 gets fixes, but no new features. 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)
{
long 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
'hello'.delete_prefix('hel') # => "lo" 'hello'.delete_prefix('llo') # => "hello" 'тест'.delete_prefix('те') # => "ст" 'こんにちは'.delete_prefix('こん') # => "にちは"
Related: String#delete_prefix!, String#delete_suffix.
Ruby version changes
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-