201

Example:

[12,23,987,43

What is the fastest, most efficient way to remove the "[", using maybe a chop() but for the first character?

4
  • 1
    I edited my answer, so it might be possible to change your selected answer. See if you can award it to Jason Stirk's answer since his is the fastest, and is very readable. Apr 22, 2013 at 18:27
  • 3
    Use str[1..-1], its fastest according to the answers below. Sep 14, 2017 at 2:57
  • 1
    As of Ruby 2.5 you can use delete_prefixand delete_prefix! - more details below. I've not had the time to benchmark, but will do soon!
    – SRack
    Oct 26, 2017 at 16:25
  • Update: I've benchmarked the new methods (delete_prefix \ delete_prefix!) and they're pretty fast. The don't quite pip the previous favourites for speed, but readability means they're great new options to have!
    – SRack
    Oct 27, 2017 at 12:34

15 Answers 15

317

Similar to Pablo's answer above, but a shade cleaner :

str[1..-1]

Will return the array from 1 to the last character.

'Hello World'[1..-1]
 => "ello World"
4
  • 13
    +1 Take a look at the benchmark results I added to my answer. You've got the fastest run-time, plus I think it's very clean. Apr 22, 2013 at 18:24
  • What about the performance of str[1,] compared to the above?
    – Bohr
    Jul 7, 2013 at 2:52
  • 1
    @Bohr: str[1,] return you the 2nd character since the range is 1:nil. You'd need to provide the actual calculated length, or something guaranteeded to be higher than length, like, str[1,999999] (use int_max of course) to get the whole tail. [1..-1] is cleaner and probably faster, since you don't need to operate on length manually (see the [1..length] in the benchmark) Aug 13, 2013 at 7:59
  • 6
    Very nice solution. By the way if one wants to remove first and last characters: str[1..-2]
    – Pikachu
    Dec 19, 2014 at 17:15
248

I kind of favor using something like:

asdf = "[12,23,987,43"
asdf[0] = '' 

p asdf
# >> "12,23,987,43"

I'm always looking for the fastest and most readable way of doing things:

require 'benchmark'

N = 1_000_000

puts RUBY_VERSION

STR = "[12,23,987,43"

Benchmark.bm(7) do |b|
  b.report('[0]') { N.times { "[12,23,987,43"[0] = '' } }
  b.report('sub') { N.times { "[12,23,987,43".sub(/^\[+/, "") } }

  b.report('gsub') { N.times { "[12,23,987,43".gsub(/^\[/, "") } }
  b.report('[1..-1]') { N.times { "[12,23,987,43"[1..-1] } }
  b.report('slice') { N.times { "[12,23,987,43".slice!(0) } }
  b.report('length') { N.times { "[12,23,987,43"[1..STR.length] } }

end

Running on my Mac Pro:

1.9.3
              user     system      total        real
[0]       0.840000   0.000000   0.840000 (  0.847496)
sub       1.960000   0.010000   1.970000 (  1.962767)
gsub      4.350000   0.020000   4.370000 (  4.372801)
[1..-1]   0.710000   0.000000   0.710000 (  0.713366)
slice     1.020000   0.000000   1.020000 (  1.020336)
length    1.160000   0.000000   1.160000 (  1.157882)

Updating to incorporate one more suggested answer:

require 'benchmark'

N = 1_000_000

class String
  def eat!(how_many = 1)
    self.replace self[how_many..-1]
  end

  def first(how_many = 1)
    self[0...how_many]
  end

  def shift(how_many = 1)
    shifted = first(how_many)
    self.replace self[how_many..-1]
    shifted
  end
  alias_method :shift!, :shift
end

class Array
  def eat!(how_many = 1)
    self.replace self[how_many..-1]
  end
end

puts RUBY_VERSION

STR = "[12,23,987,43"

Benchmark.bm(7) do |b|
  b.report('[0]') { N.times { "[12,23,987,43"[0] = '' } }
  b.report('sub') { N.times { "[12,23,987,43".sub(/^\[+/, "") } }

  b.report('gsub') { N.times { "[12,23,987,43".gsub(/^\[/, "") } }
  b.report('[1..-1]') { N.times { "[12,23,987,43"[1..-1] } }
  b.report('slice') { N.times { "[12,23,987,43".slice!(0) } }
  b.report('length') { N.times { "[12,23,987,43"[1..STR.length] } }
  b.report('eat!') { N.times { "[12,23,987,43".eat! } }
  b.report('reverse') { N.times { "[12,23,987,43".reverse.chop.reverse } }
end

Which results in:

2.1.2
              user     system      total        real
[0]       0.300000   0.000000   0.300000 (  0.295054)
sub       0.630000   0.000000   0.630000 (  0.631870)
gsub      2.090000   0.000000   2.090000 (  2.094368)
[1..-1]   0.230000   0.010000   0.240000 (  0.232846)
slice     0.320000   0.000000   0.320000 (  0.320714)
length    0.340000   0.000000   0.340000 (  0.341918)
eat!      0.460000   0.000000   0.460000 (  0.452724)
reverse   0.400000   0.000000   0.400000 (  0.399465)

And another using /^./ to find the first character:

require 'benchmark'

N = 1_000_000

class String
  def eat!(how_many = 1)
    self.replace self[how_many..-1]
  end

  def first(how_many = 1)
    self[0...how_many]
  end

  def shift(how_many = 1)
    shifted = first(how_many)
    self.replace self[how_many..-1]
    shifted
  end
  alias_method :shift!, :shift
end

class Array
  def eat!(how_many = 1)
    self.replace self[how_many..-1]
  end
end

puts RUBY_VERSION

STR = "[12,23,987,43"

Benchmark.bm(7) do |b|
  b.report('[0]') { N.times { "[12,23,987,43"[0] = '' } }
  b.report('[/^./]') { N.times { "[12,23,987,43"[/^./] = '' } }
  b.report('[/^\[/]') { N.times { "[12,23,987,43"[/^\[/] = '' } }
  b.report('sub+') { N.times { "[12,23,987,43".sub(/^\[+/, "") } }
  b.report('sub') { N.times { "[12,23,987,43".sub(/^\[/, "") } }
  b.report('gsub') { N.times { "[12,23,987,43".gsub(/^\[/, "") } }
  b.report('[1..-1]') { N.times { "[12,23,987,43"[1..-1] } }
  b.report('slice') { N.times { "[12,23,987,43".slice!(0) } }
  b.report('length') { N.times { "[12,23,987,43"[1..STR.length] } }
  b.report('eat!') { N.times { "[12,23,987,43".eat! } }
  b.report('reverse') { N.times { "[12,23,987,43".reverse.chop.reverse } }
end

Which results in:

# >> 2.1.5
# >>               user     system      total        real
# >> [0]       0.270000   0.000000   0.270000 (  0.270165)
# >> [/^./]    0.430000   0.000000   0.430000 (  0.432417)
# >> [/^\[/]   0.460000   0.000000   0.460000 (  0.458221)
# >> sub+      0.590000   0.000000   0.590000 (  0.590284)
# >> sub       0.590000   0.000000   0.590000 (  0.596366)
# >> gsub      1.880000   0.010000   1.890000 (  1.885892)
# >> [1..-1]   0.230000   0.000000   0.230000 (  0.223045)
# >> slice     0.300000   0.000000   0.300000 (  0.299175)
# >> length    0.320000   0.000000   0.320000 (  0.325841)
# >> eat!      0.410000   0.000000   0.410000 (  0.409306)
# >> reverse   0.390000   0.000000   0.390000 (  0.393044)

Here's another update on faster hardware and a newer version of Ruby:

2.3.1
              user     system      total        real
[0]       0.200000   0.000000   0.200000 (  0.204307)
[/^./]    0.390000   0.000000   0.390000 (  0.387527)
[/^\[/]   0.360000   0.000000   0.360000 (  0.360400)
sub+      0.490000   0.000000   0.490000 (  0.492083)
sub       0.480000   0.000000   0.480000 (  0.487862)
gsub      1.990000   0.000000   1.990000 (  1.988716)
[1..-1]   0.180000   0.000000   0.180000 (  0.181673)
slice     0.260000   0.000000   0.260000 (  0.266371)
length    0.270000   0.000000   0.270000 (  0.267651)
eat!      0.400000   0.010000   0.410000 (  0.398093)
reverse   0.340000   0.000000   0.340000 (  0.344077)

Why is gsub so slow?

After doing a search/replace, gsub has to check for possible additional matches before it can tell if it's finished. sub only does one and finishes. Consider gsub like it's a minimum of two sub calls.

Also, it's important to remember that gsub, and sub can also be handicapped by poorly written regex which match much more slowly than a sub-string search. If possible anchor the regex to get the most speed from it. There are answers here on Stack Overflow demonstrating that so search around if you want more information.

8
  • 34
    It's important to note that this will only work in Ruby 1.9. In Ruby 1.8, this will remove the first byte from the string, not the first character, which is not what the OP wants. Sep 1, 2010 at 8:02
  • +1: I always forget that to a string-position you can assign not only a single character, but also you can insert a substring. Thanks! Aug 13, 2013 at 8:00
  • "[12,23,987,43".delete "["
    – rupweb
    Dec 11, 2013 at 8:54
  • 4
    That deletes it from all positions, which isn't what the OP wanted: "...for the first character?". Dec 11, 2013 at 13:02
  • 2
    "what about "[12,23,987,43".shift ?"? What about "[12,23,987,43".shift NoMethodError: undefined method shift' for "[12,23,987,43":String`? Aug 28, 2014 at 18:44
55

We can use slice to do this:

val = "abc"
 => "abc" 
val.slice!(0)
 => "a" 
val
 => "bc" 

Using slice! we can delete any character by specifying its index.

3
  • 2
    This elegant slice!(0) really should be the selected answer, as using asdf[0] = '' to remove the first character is ridiculous (just like using gsub with regex and shooting a fly with a howitzer).
    – f055
    Mar 15, 2016 at 19:32
  • 1
    While it might seem unintuitive on the surface, []= doesn't require as much underlying C code, where slice! requires additional work. That adds up. The argument might be "Which is more readable?" I find using []= readable, but I'm coming from a C --> Perl background which probably colors my thinking. Java developers would probably think it's less readable. Either is an acceptable way to accomplish the task as long as it's easily understood and maintainable and doesn't load the CPU unduly. Nov 15, 2016 at 23:03
  • Ok. Do you know how we can measure up if a function takes much CPU load in ROR? or should we use execution time difference in milli or nanoseconds?
    – balanv
    Nov 19, 2016 at 11:31
47

Ruby 2.5+

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

In this case "[12,23,987,43".delete_prefix("[").

More info here:

'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"

Edit:

Using the Tin Man's benchmark setup, it looks pretty quick too (under the last two entries delete_p and delete_p!). Doesn't quite pip the previous faves for speed, though is very readable.

2.5.0
              user     system      total        real
[0]       0.174766   0.000489   0.175255 (  0.180207)
[/^./]    0.318038   0.000510   0.318548 (  0.323679)
[/^\[/]   0.372645   0.001134   0.373779 (  0.379029)
sub+      0.460295   0.001510   0.461805 (  0.467279)
sub       0.498351   0.001534   0.499885 (  0.505729)
gsub      1.669837   0.005141   1.674978 (  1.682853)
[1..-1]   0.199840   0.000976   0.200816 (  0.205889)
slice     0.279661   0.000859   0.280520 (  0.285661)
length    0.268362   0.000310   0.268672 (  0.273829)
eat!      0.341715   0.000524   0.342239 (  0.347097)
reverse   0.335301   0.000588   0.335889 (  0.340965)
delete_p  0.222297   0.000832   0.223129 (  0.228455)
delete_p!  0.225798   0.000747   0.226545 (  0.231745)
0
18

I prefer this:

str = "[12,23,987,43"
puts str[1..-1]
>> 12,23,987,43
1
14

If you always want to strip leading brackets:

"[12,23,987,43".gsub(/^\[/, "")

If you just want to remove the first character, and you know it won't be in a multibyte character set:

"[12,23,987,43"[1..-1]

or

"[12,23,987,43".slice(1..-1)
2
  • 1
    I'd use "[12,23,987,43".sub(/^\[+/, "") instead of gsub(/^\[/, ""). The first lets the regex engine find all matches then they're replaced in one action and results in about a 2x improvement in speed with Ruby 1.9.3. Apr 22, 2013 at 16:11
  • 1
    Since we're dealing with strings, should this be gsub(/\A\[/, "") ? Jun 18, 2017 at 14:07
8

Inefficient alternative:

str.reverse.chop.reverse
4

For example : a = "One Two Three"

1.9.2-p290 > a = "One Two Three"
 => "One Two Three" 

1.9.2-p290 > a = a[1..-1]
 => "ne Two Three" 

1.9.2-p290 > a = a[1..-1]
 => "e Two Three" 

1.9.2-p290 > a = a[1..-1]
 => " Two Three" 

1.9.2-p290 > a = a[1..-1]
 => "Two Three" 

1.9.2-p290 > a = a[1..-1]
 => "wo Three" 

In this way you can remove one by one first character of the string.

1
3

Easy way:

str = "[12,23,987,43"

removed = str[1..str.length]

Awesome way:

class String
  def reverse_chop()
    self[1..self.length]
  end
end

"[12,23,987,43".reverse_chop()

(Note: prefer the easy way :) )

4
  • 1
    If you want to preserve the "chop" semantics you could just "[12,23,987,43".reverse.chop.reverse Sep 1, 2010 at 1:24
  • that is a pretty big performance overhead just to strip one char Sep 1, 2010 at 2:16
  • 7
    why not use [1..-1] rather than [1..self.length] ?
    – horseyguy
    Sep 1, 2010 at 3:59
  • Monkey patching example is pretty off for this question, it is just irrelevant and ugly IMO.
    – dredozubov
    Mar 17, 2014 at 19:55
3

Thanks to @the-tin-man for putting together the benchmarks!

Alas, I don't really like any of those solutions. Either they require an extra step to get the result ([0] = '', .strip!) or they aren't very semantic/clear about what's happening ([1..-1]: "Um, a range from 1 to negative 1? Yearg?"), or they are slow or lengthy to write out (.gsub, .length).

What we are attempting is a 'shift' (in Array parlance), but returning the remaining characters, rather than what was shifted off. Let's use our Ruby to make this possible with strings! We can use the speedy bracket operation, but give it a good name, and take an arg to specify how much we want to chomp off the front:

class String
  def eat!(how_many = 1)
    self.replace self[how_many..-1]
  end
end

But there is more we can do with that speedy-but-unwieldy bracket operation. While we are at it, for completeness, let's write a #shift and #first for String (why should Array have all the fun‽‽), taking an arg to specify how many characters we want to remove from the beginning:

class String
  def first(how_many = 1)
    self[0...how_many]
  end

  def shift(how_many = 1)
    shifted = first(how_many)
    self.replace self[how_many..-1]
    shifted
  end
  alias_method :shift!, :shift
end

Ok, now we have a good clear way of pulling characters off the front of a string, with a method that is consistent with Array#first and Array#shift (which really should be a bang method??). And we can easily get the modified string as well with #eat!. Hm, should we share our new eat!ing power with Array? Why not!

class Array
  def eat!(how_many = 1)
    self.replace self[how_many..-1]
  end
end

Now we can:

> str = "[12,23,987,43" #=> "[12,23,987,43"
> str.eat!              #=> "12,23,987,43"
> str                   #=> "12,23,987,43"

> str.eat!(3)           #=> "23,987,43"
> str                   #=> "23,987,43"

> str.first(2)          #=> "23"
> str                   #=> "23,987,43"

> str.shift!(3)         #=> "23,"
> str                   #=> "987,43"

> arr = [1,2,3,4,5]     #=> [1, 2, 3, 4, 5] 
> arr.eat!              #=> [2, 3, 4, 5] 
> arr                   #=> [2, 3, 4, 5] 

That's better!

1
  • 2
    I remember a discussion years ago in Perl forums about such a function with the name of chip() instead of chop() (and chimp() as the analog of chomp()). Jun 22, 2014 at 20:45
2
str = "[12,23,987,43"

str[0] = ""
1
  • 7
    It's important to note that this will only work in Ruby 1.9. In Ruby 1.8, this will remove the first byte from the string, not the first character, which is not what the OP wants. Sep 1, 2010 at 8:03
0
class String
  def bye_felicia()
    felicia = self.strip[0] #first char, not first space.
    self.sub(felicia, '')
  end
end
1
  • What did Felicia do to you to deserve this code Haiku? Feb 18, 2021 at 5:36
0

Using regex:

str = 'string'
n = 1  #to remove first n characters

str[/.{#{str.size-n}}\z/] #=> "tring"
0

I find a nice solution to be str.delete(str[0]) for its readability, though I cannot attest to it's performance.

0

list = [1,2,3,4] list.drop(1)

# => [2,3,4]

List drops one or more elements from the start of the array, does not mutate the array, and returns the array itself instead of the dropped element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.