Ruby-on-rails 删除字符串第一个字符的最有效方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8217993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the most efficient way to remove first character of a string?
提问by user1049097
I have a string c1234-- what's the most efficient and quick way to remove the first letter of a string?
我有一个字符串c1234——删除字符串第一个字母的最有效和最快捷的方法是什么?
回答by halfdan
Use slice!:
使用slice!:
s = "Hello"
s.slice!(0) #=> "ello"
Try it in an irb:
试试吧irb:
ruby-1.9.3-p0 :001 > s = "Hello"
=> "Hello"
ruby-1.9.3-p0 :002 > s.slice!(0) #=> "ello"
=> "H"
ruby-1.9.3-p0 :003 > s
=> "ello"
回答by Amir Raminfar
Best solution is going to be "foobar"[1..-1]. No regex needed.
最佳解决方案将是"foobar"[1..-1]. 不需要正则表达式。
回答by KL-7
Speaking of efficiency I never had a good chance to play with ruby's Benchmarkmodule so decided to do it out of curiosity right now. Here's benchmark:
说到效率,我从来没有很好的机会使用 ruby 的Benchmark模块,所以现在出于好奇决定这样做。这是基准:
require 'benchmark'
n = 10_000_000
s = 'c1234'
Benchmark.bm(8) do |x|
x.report('slice!') { n.times { s.dup.slice!(0) } }
x.report('slice') { n.times { s.dup.slice(1, 4) } }
x.report('[1..-1]') { n.times { s.dup[1..-1] } }
x.report('[1..4]') { n.times { s.dup[1..4] } }
x.report('reverse') { n.times { s.dup.reverse.chop.reverse } }
x.report('gsub') { n.times { s.dup.gsub(/^./, "") } }
x.report('sub') { n.times { s.dup.sub(/^./, "") } }
end
And there are results:
并且有结果:
user system total real
slice! 7.460000 0.000000 7.460000 (7.493322)
slice 6.880000 0.000000 6.880000 (6.902811)
[1..-1] 7.710000 0.000000 7.710000 (7.728741)
[1..4] 7.700000 0.000000 7.700000 (7.717171)
reverse 10.130000 0.000000 10.130000 (10.151716)
gsub 11.030000 0.000000 11.030000 (11.051068)
sub 9.860000 0.000000 9.860000 (9.880881)
Seems like sliceis the best choice with the most obvious (at least for me) s[1..-1]or s[1..4]a little behind. And solutions with reverseand regexp looks to complex for that kind of task.
似乎slice是最好的选择,最明显(至少对我而言)s[1..-1]或s[1..4]稍稍落后。使用reverse和 regexp 的解决方案对于这种任务来说看起来很复杂。
回答by Waldo Uribe Fache
Option 1:
选项1:
"abcd"[1..-1]
Option 2 (More descriptive):
选项 2(更具描述性):
"abcd".last(-1)
回答by WarHog
There is more than one way to do it :)
有不止一种方法可以做到:)
"foobar".gsub(/^./, "") # => "oobar"
回答by knut
I took the solutions up to now and cretaed a benchmark:
到目前为止,我采用了解决方案并创建了一个基准:
require 'benchmark'
#~ TEST_LOOPS = 10_000_000
TEST_LOOPS = 10_000
TESTSTRING = 'Hello'
Benchmark.bmbm(10) {|b|
b.report('slice!') {
TEST_LOOPS.times {
s = TESTSTRING.dup
s.slice!(0)
} #Testloops
} #b.report
b.report('gsub^') {
TEST_LOOPS.times {
s = TESTSTRING.dup
s.gsub(/^./, "")
} #Testloops
} #b.report
b.report('gsub\A') {
TEST_LOOPS.times {
s = TESTSTRING.dup
s.gsub(/\A./, "")
} #Testloops
} #b.report
b.report('[1..-1]') {
TEST_LOOPS.times {
s = TESTSTRING.dup
s = s[1..-1]
} #Testloops
} #b.report
b.report('s[0] = ""') {
TEST_LOOPS.times {
s = TESTSTRING.dup
s[0] = ''
} #Testloops
} #b.report
b.report('reverse.chop') {
TEST_LOOPS.times {
s = TESTSTRING.dup
s = s.reverse.chop.reverse
} #Testloops
} #b.report
} #Benchmark
Result:
结果:
Rehearsal ------------------------------------------------
slice! 0.000000 0.000000 0.000000 ( 0.000000)
gsub^ 0.063000 0.000000 0.063000 ( 0.062500)
gsub\A 0.031000 0.000000 0.031000 ( 0.031250)
[1..-1] 0.000000 0.000000 0.000000 ( 0.000000)
s[0] = "" 0.015000 0.000000 0.015000 ( 0.015625)
reverse.chop 0.016000 0.000000 0.016000 ( 0.015625)
--------------------------------------- total: 0.125000sec
user system total real
slice! 0.016000 0.000000 0.016000 ( 0.015625)
gsub^ 0.046000 0.000000 0.046000 ( 0.046875)
gsub\A 0.032000 0.000000 0.032000 ( 0.031250)
[1..-1] 0.015000 0.000000 0.015000 ( 0.015625)
s[0] = "" 0.016000 0.000000 0.016000 ( 0.015625)
reverse.chop 0.016000 0.000000 0.016000 ( 0.015625)
At least the regular expresions should not be used.
至少不应该使用正则表达式。
回答by ramblex
If you're using ruby 1.9:
如果您使用的是 ruby 1.9:
s = "foobar"
s[0] = ''

