Project

General

Profile

Actions

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.

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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0