Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
String#delete_suffix
delete_suffix(suffix) → new_string
string.c
View on GitHub ↗
static VALUE
rb_str_delete_suffix(VALUE str, VALUE suffix)
{
long suffixlen;
suffixlen = deleted_suffix_length(str, suffix);
if (suffixlen <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, 0, RSTRING_LEN(str) - suffixlen);
}
Returns a copy of self with trailing substring suffix removed:
Example
'hello'.delete_suffix('llo') # => "he" 'hello'.delete_suffix('hel') # => "hello" 'тест'.delete_suffix('ст') # => "те" 'こんにちは'.delete_suffix('ちは') # => "こんに"
Related: String#delete_suffix!, String#delete_prefix.
Ruby version changes
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-
See also
#delete_suffix!
#delete_prefix!
#delete_prefix
#downcase
also in
4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0