从字符串中删除第一个字符的最简单方法是什么?

时间:2010-09-01 01:13:51

标签: ruby string

示例:

[12,23,987,43

删除“[”的最快,最有效的方法是什么, 使用chop()但是第一个字符?

15 个答案:

答案 0 :(得分:273)

与Pablo上面的答案类似,但是更清洁:

str[1..-1]

将数组从1返回到最后一个字符。

'Hello World'[1..-1]
 => "ello World"

答案 1 :(得分:217)

我喜欢使用类似的东西:

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

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

我一直在寻找最快,最易读的做事方式:

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

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

更新以包含一个建议的答案:

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

结果是:

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)

另一个使用/^./找到第一个字符:

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

结果是:

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

这是关于更快的硬件和更新版本的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)

  

为什么gsub这么慢?

执行搜索/替换后,gsub必须检查可能的其他匹配,然后才能判断它是否已完成。 sub只做一个并完成。请考虑gsub,因为它至少需要两次sub次呼叫。

此外,重要的是要记住,gsubsub也可以被写得不好的正则表达式所阻碍,这种正则表达式匹配比子字符串搜索慢得多。如果可能的话,锚定正则表达式以从中获得最大速度。 Stack Overflow上有答案,证明如果你想了解更多信息,那就四处寻找。

答案 2 :(得分:47)

我们可以使用切片来做到这一点:

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

使用slice!我们可以通过指定索引来删除任何字符。

答案 3 :(得分:14)

如果你总是想剥去前导括号:

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

如果您只想删除第一个字符,并且您知道它不会是多字节字符集:

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

"[12,23,987,43".slice(1..-1)

答案 4 :(得分:14)

我更喜欢这个

srt = [12,23,987,43
p srt[1..-1]
>> 12,23,987,43

答案 5 :(得分:9)

Ruby 2.5 +

从Ruby 2.5开始,您可以使用delete_prefixdelete_prefix!以可读的方式实现此目的。

在这种情况下"[12,23,987,43".delete_prefix("[")

更多信息:

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。您还可以使用此功能从delete_suffixdelete_suffix!

中删除字符串末尾的项目
'worked'.delete_suffix('ed') #=> "work"
'medical'.delete_suffix('ed') #=> "medical"

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

编辑:

使用Tin Man的基准设置,它看起来也很快(在最后两个条目delete_pdelete_p!下)。 完全不是为了提高速度,而是非常可读

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)

答案 6 :(得分:6)

低效的替代方案:

str.reverse.chop.reverse

答案 7 :(得分:4)

例如:a =“一二三”

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" 

通过这种方式,您可以逐个删除字符串的第一个字符。

答案 8 :(得分:3)

简单方法:

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

removed = str[1..str.length]

很棒的方式:

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

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

(注意:更喜欢简单的方法:))

答案 9 :(得分:3)

感谢@ the-tin-man组合基准!

唉,我真的不喜欢这些解决方案。要么他们需要一个额外的步骤来获得结果([0] = ''.strip!),要么他们对发生的事情没有非常语义/清晰([1..-1]:“嗯,从1到1的范围否定1?年份?“),或写出来的时间很慢或很长(.gsub.length)。

我们正在尝试的是“移位”(在数组术语中),但返回剩余的字符,而不是移出的字符。让我们使用我们的Ruby来实现字符串!我们可以使用快速括号操作,但是给它一个好名字,然后用arg指定我们想要从前面榨取多少:

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

但我们可以通过快速但笨拙的支架操作做更多事情。虽然我们在这里,但为了完整性,让我们为String写一个#shift#first(为什么Array应该有所有的乐趣),用arg来指定我们要删除多少个字符一开始:

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

好的,现在我们有一个很好的清晰方法可以将字符从字符串前面拉出来,方法与Array#firstArray#shift一致(这真的应该是一个爆炸方法? )。我们也可以使用#eat!轻松获取修改后的字符串。嗯,我们应该与Array共享新的eat!功能吗?为什么不!

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

现在我们可以:

> 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] 

那更好!

答案 10 :(得分:2)

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

str[0] = ""

答案 11 :(得分:0)

class String
  def bye_felicia()
    felicia = self.strip[0] #first char, not first space.
    self.sub(felicia, '')
  end
end

答案 12 :(得分:0)

使用正则表达式:

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

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

答案 13 :(得分:0)

我找到了一个很好的解决方案,因为它的可读性是str.delete(str[0]),但我无法证明它的性能。

答案 14 :(得分:0)

  

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

     

# => [2,3,4]

List从数组的开头删除一个或多个元素,不对数组进行突变,并返回数组本身而不是被删除的元素。