从字符串中删除最后n个字符的首选方法是什么?
当前回答
删除最后n个字符与保留第一个长度- n个字符相同。
主动支持包括String#first和String#last方法,它们提供了一种方便的方法来保留或删除第一个/最后一个n个字符:
require 'active_support/core_ext/string/access'
"foobarbaz".first(3) # => "foo"
"foobarbaz".first(-3) # => "foobar"
"foobarbaz".last(3) # => "baz"
"foobarbaz".last(-3) # => "barbaz"
其他回答
str = str[0..-1-n]
不像[0…-n],它处理n=0的情况。
如果你可以创建类方法,想要删除字符,试试这个:
class String
def chop_multiple(amount)
amount.times.inject([self, '']){ |(s, r)| [s.chop, r.prepend(s[-1])] }
end
end
hello, world = "hello world".chop_multiple 5
hello #=> 'hello '
world #=> 'world'
如果你想要删除的字符总是相同的字符,那么考虑chomp:
'abc123'.chomp('123') # => "abc"
chomp的优点是:无需计数,代码可以更清楚地传达它正在做什么。
如果没有参数,chomp将删除DOS或Unix行结束符,如果它们都存在:
"abc\n".chomp # => "abc"
"abc\r\n".chomp # => "abc"
从评论中可以看出,使用#chomp和使用range的速度有什么不同。以下是两者的比较基准:
require 'benchmark'
S = 'asdfghjkl'
SL = S.length
T = 10_000
A = 1_000.times.map { |n| "#{n}#{S}" }
GC.disable
Benchmark.bmbm do |x|
x.report('chomp') { T.times { A.each { |s| s.chomp(S) } } }
x.report('range') { T.times { A.each { |s| s[0...-SL] } } }
end
基准测试结果(使用CRuby 2.13p242):
Rehearsal -----------------------------------------
chomp 1.540000 0.040000 1.580000 ( 1.587908)
range 1.810000 0.200000 2.010000 ( 2.011846)
-------------------------------- total: 3.590000sec
user system total real
chomp 1.550000 0.070000 1.620000 ( 1.610362)
range 1.970000 0.170000 2.140000 ( 2.146682)
因此,chomp比使用range快22%。
查看slice()方法:
http://ruby-doc.org/core-2.5.0/String.html#method-i-slice
irb> 'now is the time'[0...-4]
=> "now is the "
推荐文章
- 我应该如何从字符串中删除所有的前导空格?- - - - - -斯威夫特
- 了解Gemfile。锁文件
- 如何确定一个数组是否包含另一个数组的所有元素
- 将整数转换为字符串,以逗号表示千
- 将JavaScript字符串中的多个空格替换为单个空格
- printf()和puts()在C语言中的区别是什么?
- 是什么导致这个ActiveRecord::ReadOnlyRecord错误?
- jQuery -替换字符串中某个字符的所有实例
- Base64长度计算?
- 使用split("|")按管道符号拆分Java字符串
- 最好的打印散列的方法
- Std::cin输入空格?
- Rails:dependent =>:destroy VS:dependent =>:delete_all
- 我可以在不包含Ruby模块的情况下调用实例方法吗?
- 如何将新项目添加到哈希