33

How can I remove the very first "1" from any string if that string starts with a "1"?

"1hello world" => "hello world"
"112345" => "12345"

I'm thinking of doing

string.sub!('1', '') if string =~ /^1/

but I' wondering there's a better way. Thanks!

1

6 Answers 6

46

Why not just include the regex in the sub! method?

string.sub!(/^1/, '')
0
30

As of Ruby 2.5 you can use delete_prefix or delete_prefix! to achieve this in a readable manner.

In this case "1hello world".delete_prefix("1").

More info here:

https://blog.jetbrains.com/ruby/2017/10/10-new-features-in-ruby-2-5/

https://bugs.ruby-lang.org/issues/12694

'invisible'.delete_prefix('in') #=> "visible"
'pink'.delete_prefix('in') #=> "pink"

N.B. you can also use this to remove items from the end of a string with delete_suffix and delete_suffix!

'worked'.delete_suffix('ed') #=> "work"
'medical'.delete_suffix('ed') #=> "medical"

https://bugs.ruby-lang.org/issues/13665

I've answered in a little more detail (with benchmarks) here: What is the easiest way to remove the first character from a string?

8

if you're going to use regex for the match, you may as well use it for the replacement

string.sub!(%r{^1},"")

BTW, the %r{} is just an alternate syntax for regular expressions. You can use %r followed by any character e.g. %r!^1!.

2
  • 2
    "BTW, the %r{} syntax for regular expressions allows you to avoid escaping / within the expression." - But now you have two characters that you need to escape instead of one but the point is moot since none of the characters in question even appear in the pattern. Dec 16, 2008 at 2:53
  • It's just a subjective preference. I'd argue that / is a more common character than either {}. My BTW was an explanation in case the OP hadn't seen %r{} syntax before. Dec 16, 2008 at 3:13
3

Careful using sub!(/^1/,'') ! In case the string doesn't match /^1/ it will return nil. You should probably use sub (without the bang).

1
  • 1
    No, it will return nil in that case, but the original string will remain unchanged. If you just use sub, you will have to do the longer string = string.sub(/^/, ''). Dec 16, 2008 at 19:05
1

This answer might be more optimised: What is the easiest way to remove the first character from a string?

string[0] = '' if string[0] == '1'

1
  • 1
    Don't know about optimised, but this seems like the most readable to solution to me. I must be just weird finding regex solutions ugly!
    – Harry Wood
    Jul 28, 2020 at 14:22
0

I'd like to post a tiny improvement to the otherwise excellent answer by Zach. The ^ matches the beginning of every line in Ruby regex. This means there can be multiple matches per string. Kenji asked about the beginning of the string which means they have to use this regex instead:

string.sub!(/\A1/, '')

Compare this - multiple matches with this - one match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.