Project

General

Profile

Actions

Feature #12694

closed

Want a String method to remove heading substr

Added by sonots (Naotoshi Seo) over 7 years ago. Updated almost 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:77016]

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 open0 closed)

Is duplicate of Ruby master - Feature #6842: Add Optional Arguments to String#stripAssignedmatz (Yukihiro Matsumoto)Actions

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

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.

Updated by sonots (Naotoshi Seo) over 7 years ago

Thanks, I found an issue which is 3 years old https://bugs.ruby-lang.org/issues/6842

Actions #3

Updated by shyouhei (Shyouhei Urabe) over 7 years ago

  • Is duplicate of Feature #6842: Add Optional Arguments to String#strip added

Updated by sonots (Naotoshi Seo) over 7 years ago

  • Status changed from Open to Rejected

I will continue this issue on https://bugs.ruby-lang.org/issues/6842, let me close.

=> https://bugs.ruby-lang.org/issues/6842 is too broad. Let us reopen this ticket, and talk about a way removing heading substr at this thread.

Updated by sonots (Naotoshi Seo) over 7 years ago

  • Status changed from Rejected to Open

Updated by mrkn (Kenta Murata) over 7 years ago

This method removes prefix string, so I propose remove_prefix or deprefix (means remove prefix).

Updated by sonots (Naotoshi Seo) over 7 years ago

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?

Updated by sonots (Naotoshi Seo) over 7 years ago

It seems python, perl does not have similar methods (which removes heading substr)

Updated by sonots (Naotoshi Seo) over 7 years ago

  • Description updated (diff)

Updated by sonots (Naotoshi Seo) over 7 years ago

  • Description updated (diff)

Updated by sonots (Naotoshi Seo) over 7 years ago

  • Description updated (diff)

Updated by sonots (Naotoshi Seo) over 7 years ago

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

Updated by duerst (Martin Dürst) over 7 years ago

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.

Updated by sonots (Naotoshi Seo) almost 7 years ago

I've created a PR https://github.com/ruby/ruby/pull/1632 with a name String#remove_prefix.
I will merge if the name is acceptable.

Updated by znz (Kazuhiro NISHIYAMA) almost 7 years ago

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

Updated by sonots (Naotoshi Seo) almost 7 years ago

\A is correct. I updated the description, thanks.

Actions #17

Updated by sonots (Naotoshi Seo) almost 7 years ago

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) almost 7 years ago

I'd pick the name delete_prefix.

Matz.

Updated by sonots (Naotoshi Seo) almost 7 years ago

Thanks! I've changed the name to delete_prefix. https://github.com/ruby/ruby/pull/1632

Updated by Hanmac (Hans Mackowiak) almost 7 years ago

sonots (Naotoshi Seo) wrote:

Thanks! I've changed the name to delete_prefix. https://github.com/ruby/ruby/pull/1632

do we need a delete_suffix too or is that done via another method?

Updated by sonots (Naotoshi Seo) almost 7 years ago

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.

Updated by sonots (Naotoshi Seo) almost 7 years ago

I made a ticket for String#delete_suffix https://bugs.ruby-lang.org/issues/13665

Actions #23

Updated by sonots (Naotoshi Seo) almost 7 years ago

  • 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0