views:

74

answers:

5

Example: "[12,23,987,43"

What is the fastest, most efficient way to remove the "[" ?

Like, maybe a chop() but for the first char.

+1  A: 

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 :) )

Pablo Fernandez
If you want to preserve the "chop" semantics you could just `"[12,23,987,43".reverse.chop.reverse`
Chris Heald
that is a pretty big performance overhead just to strip one char
Pablo Fernandez
why not use [1..-1] rather than [1..self.length] ?
banister
+2  A: 

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)
Chris Heald
+2  A: 

I kind of favor using something like:

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

p asdf
# >> "12,23,987,43"
Greg
+1 for easiest way
macek
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.
Jörg W Mittag
+1  A: 
str = "[12,23,987,43"

str[0] = ""
Handcraftsman
+1 for easiest way
macek
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.
Jörg W Mittag
+1  A: 

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"
Jason Stirk