instance method
String#delete_suffix
(::string suffix) -> ::String
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
'foo'.delete_suffix('o') # => "fo" 'foo'.delete_suffix('oo') # => "f" 'foo'.delete_suffix('foo') # => "" 'foo'.delete_suffix('f') # => "foo" 'foo'.delete_suffix('x') # => "foo" 'тест'.delete_suffix('ст') # => "те" 'こんにちは'.delete_suffix('ちは') # => "こんに"
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]
-
See also
#delete_suffix!
#delete_prefix!
#delete_prefix
#downcase
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0