Feature #12694
closed
Want a String method to remove heading substr
Added by sonots (Naotoshi Seo) about 8 years ago.
Updated over 7 years ago.
Description
I often write codes like:
str = 'abcdef'
substr = 'abc'
str[substr.size..-1] if str.start_with?(substr) #=> 'def'
# or
str.sub(/\A#{Regexp.escape(substr)}/, '') #=> 'def'
I want a short hand which is something like:
str = 'abcdef'
substr = 'abc'
str.lstrip(substr) #=> 'def'
Having similar argument for String#rstrip
would be nice for symmetry although we already have String#chomp(substr)
My frequent use-case is coming from fluentd. We have remove_tag_prefix
option to remove a heading substring from a tag. We currently using String#sub
with a regular expression, but I feel regular expression matching is too much just to remove a heading substring. https://github.com/fluent/fluentd/blob/75005f18d48ed3d416890413fa5f83982b264c71/lib/fluent/compat/handle_tag_name_mixin.rb#L45
Related issues
1 (1 open — 0 closed)
IIRC, there were proposals of lstrip
and rstrip
enhancement.
I'd expect them to accept char class(es), than a sub-string.
Note that rb_str_index_m
accepts a Regexp
too.
Calling str_strlen
on it will segfault.
- Is duplicate of Feature #6842: Add Optional Arguments to String#strip added
- Status changed from Open to Rejected
- Status changed from Rejected to Open
This method removes prefix string, so I propose remove_prefix
or deprefix
(means remove prefix).
We discussed about this ticket on ruby development meeting.
Python lstrip http://www.tutorialspoint.com/python/string_lstrip.htm takes character classes as an argument, so providing a substring as an argument will introduce confusion.
We need to think of new naming. What about String#lchomp
or String#remove_prefix
or String#head_chop
for example?
It seems python, perl does not have similar methods (which removes heading substr)
- Description updated (diff)
- Description updated (diff)
- Description updated (diff)
PHP has ltrim and rtrim (which is an alias of chop), but they are for removing a character list, not removing a substring http://php.net/manual/en/function.ltrim.php
ltrim('foaofe', 'foa'); #=> 'e'
Elixir has trim_leading, but it looks different with what we want to do http://elixir-lang.org/docs/stable/elixir/String.html#trim_leading/2
iex> String.trim_leading("__ abc _", "_")
" abc _"
iex> String.trim_leading("1 abc", "11")
"1 abc"
Golang has the same one with what we want to do https://golang.org/pkg/strings/#TrimPrefix and TrimSuffix
var s = "Goodbye,, world!"
s = strings.TrimPrefix(s, "Goodbye,") #=> , world
Naotoshi Seo wrote:
Python lstrip http://www.tutorialspoint.com/python/string_lstrip.htm takes character classes as an argument, so providing a substring as an argument will introduce confusion.
"character classes" may be confusing. "a list of characters" might be more precise. There are many classes of characters that can't be expressed easily with a list. In particular because Python lstrip doesn't accept hyphens to group characters.
One way to deal with this would be to allow a Regexp that selects a single character, and apply this repeatedly. But we don't have any such concept yet.
str.sub(/^#{Regexp.escape(substr)}/, '') #=> 'def'
I think ^
should be \A
in description of this ticket. (and maybe in handle_tag_name_mixin.rb of fluentd too)
Which is expected?: first prefix of string only or prefix of each line
\A is correct. I updated the description, thanks.
- Description updated (diff)
I'd pick the name delete_prefix
.
Matz.
We have String#chomp to delete suffix, but it would be nice to have the method for symmetry.
I will create another thread to discuss about it.
- Status changed from Open to Closed
Applied in changeset trunk|r59132.
string.c: add String#delete_prefix and String#delete_prefix!
to remove leading substr [Feature #12694] [fix GH-1632]
-
string.c (rb_str_delete_prefix_bang): add a new method
to remove prefix destuctively.
-
string.c (rb_str_delete_prefix): add a new method
to remove prefix non-destuctively.
-
test/ruby/test_string.rb: add tests.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0