去除所有空白的Ruby函数是什么?我正在寻找一些类似于PHP的trim()?
当前回答
你可以试试这个:
"ab c d efg hi ".split.map(&:strip)
为了得到这个:
["ab, "c", "d", "efg", "hi"]
或者如果你想要一个字符串,只需使用:
"ab c d efg hi ".split.join
其他回答
我会用这样的方法:
my_string = "Foo bar\nbaz quux"
my_string.split.join
=> "Foobarbazquux"
你可以试试这个
"Some Special Text Values".gsub(/[[:space:]]+/, "")
使用:space:删除不间断的空格和常规空格。
对于完全匹配PHP trim的行为,最简单的方法是使用String#strip方法,如下所示:
string = " Many have tried; many have failed! "
puts "Original [#{string}]:#{string.length}"
new_string = string.strip
puts "Updated [#{new_string}]:#{new_string.length}"
Ruby也有一个就地编辑的版本,叫做String.strip!(注意后面的“!”)。这不需要创建字符串的副本,并且对于某些用途可以显著地更快:
string = " Many have tried; many have failed! "
puts "Original [#{string}]:#{string.length}"
string.strip!
puts "Updated [#{string}]:#{string.length}"
两个版本都会产生如下输出:
Original [ Many have tried; many have failed! ]:40
Updated [Many have tried; many have failed!]:34
我创建了一个基准测试性能的一些基本用途的长条和长条!,以及一些替代方案。测试是这样的:
require 'benchmark'
string = 'asdfghjkl'
Times = 25_000
a = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }
b = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }
c = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }
d = Times.times.map {|n| spaces = ' ' * (1+n/4); "#{spaces}#{spaces}#{string}#{spaces}" }
puts RUBY_DESCRIPTION
puts "============================================================"
puts "Running tests for trimming strings"
Benchmark.bm(20) do |x|
x.report("s.strip:") { a.each {|s| s = s.strip } }
x.report("s.rstrip.lstrip:") { a.each {|s| s = s.rstrip.lstrip } }
x.report("s.gsub:") { a.each {|s| s = s.gsub(/^\s+|\s+$/, "") } }
x.report("s.sub.sub:") { a.each {|s| s = s.sub(/^\s+/, "").sub(/\s+$/, "") } }
x.report("s.strip!") { a.each {|s| s.strip! } }
x.report("s.rstrip!.lstrip!:") { b.each {|s| s.rstrip! ; s.lstrip! } }
x.report("s.gsub!:") { c.each {|s| s.gsub!(/^\s+|\s+$/, "") } }
x.report("s.sub!.sub!:") { d.each {|s| s.sub!(/^\s+/, "") ; s.sub!(/\s+$/, "") } }
end
结果如下:
ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-darwin14]
============================================================
Running tests for trimming strings
user system total real
s.strip: 2.690000 0.320000 3.010000 ( 4.048079)
s.rstrip.lstrip: 2.790000 0.060000 2.850000 ( 3.110281)
s.gsub: 13.060000 5.800000 18.860000 ( 19.264533)
s.sub.sub: 9.880000 4.910000 14.790000 ( 14.945006)
s.strip! 2.750000 0.080000 2.830000 ( 2.960402)
s.rstrip!.lstrip!: 2.670000 0.320000 2.990000 ( 3.221094)
s.gsub!: 13.410000 6.490000 19.900000 ( 20.392547)
s.sub!.sub!: 10.260000 5.680000 15.940000 ( 16.411131)
其实有一种更短更容易理解的方法。
为什么不直接分拆加入呢?
"s t r i n g".split(" ").join()
别忘了:
$ s = " I have white space ".split
=> ["I", "have", "white", "space"]
推荐文章
- Printf与std::字符串?
- 是否可以在MiniTest中运行单个测试?
- 如何在Ruby中生成a和b之间的随机数?
- 不区分大小写的“in”
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 如何在PHP中截断字符串最接近于一定数量的字符?
- 无法安装gem -未能建立gem本地扩展-无法加载这样的文件——mkmf (LoadError)
- 如何在Ruby中创建文件
- 什么是Ruby文件。开放模式和选项?
- Ruby数组到字符串的转换
- 如何分割(块)一个Ruby数组成X元素的部分?
- Ruby中“or”和||的区别?
- 为什么在Java和。net中不能修改字符串?
- 如何创建一个日期对象从字符串在javascript
- 如何测试参数是否存在在轨道