如何在Ruby中从字符串中获取第一行?
我在Ruby中有一个字符串变量,如下所示:如何在Ruby中从字符串中获取第一行?,ruby,Ruby,我在Ruby中有一个字符串变量,如下所示: puts $varString.class puts "##########" puts $varString 上述代码的输出为: String ########## my::FIrst::Line this id second line sjdf kjsdfh jsdf djsf sdk fxdj 我只需要从字符串变量中获取第一行(例如,my::first::line)。 如何获取它?\Ruby>=1.8.7 # Ruby >=
puts $varString.class
puts "##########"
puts $varString
上述代码的输出为:
String
##########
my::FIrst::Line
this id second line
sjdf kjsdfh jsdf
djsf sdk fxdj
我只需要从字符串变量中获取第一行(例如,my::first::line
)。
如何获取它?\Ruby>=1.8.7
# Ruby >= 1.8.7
$varString.lines.first
# => "my::FIrst::Line"
# Ruby < 1.8.7
$varString.split("\n").first
# => "my::FIrst::Line"
$varString.lines.first
#=>“我的::第一个::行”
#Ruby<1.8.7
$varString.split(“\n”)。首先
#=>“我的::第一个::行”
作为旁注,请避免使用全局变量($
符号)
puts $varString.split('\n')[0]
拆分“\n”标记上的字符串,并获取第一个标记
$varString.lines.first
或者,如果要在结果字符串中去掉最后一个换行符:
$varString.lines.first.chomp
str=替代@steenslag的答案是使用StringIO只获取第一行
str = <<DOC1
asrg
aeg
aegfr
DOC1
puts StringIO.open(str, &:readline)
str=第一行=str[/.*/]
就内存分配和性能而言,此解决方案似乎是最有效的解决方案
这使用了str[regexp]
表单,请参见
基准代码:
require 'stringio'
require 'benchmark/ips'
require 'benchmark/memory'
str = "test\n" * 100
Benchmark.ips do |x|
x.report('regex') { str[/.*/] }
x.report('index') { str[0..(str.index("\n") || -1)] }
x.report('stringio') { StringIO.open(str, &:readline) }
x.report('each_line') { str.each_line.first.chomp }
x.report('lines') { str.lines.first }
x.report('split') { str.split("\n").first }
x.compare!
end
Benchmark.memory do |x|
x.report('regex') { str[/.*/] }
x.report('index') { str[0..(str.index("\n") || -1)] }
x.report('stringio') { StringIO.open(str, &:readline) }
x.report('each_line') { str.each_line.first.chomp }
x.report('lines') { str.lines.first }
x.report('split') { str.split("\n").first }
x.compare!
end
Comparison:
regex: 5099725.8 i/s
index: 4968096.7 i/s - 1.03x slower
stringio: 3001260.8 i/s - 1.70x slower
each_line: 2330869.5 i/s - 2.19x slower
lines: 187918.5 i/s - 27.14x slower
split: 182865.6 i/s - 27.89x slower
Comparison:
regex: 40 allocated
index: 120 allocated - 3.00x more
stringio: 120 allocated - 3.00x more
each_line: 208 allocated - 5.20x more
lines: 5064 allocated - 126.60x more
split: 5104 allocated - 127.60x more
基准测试结果:
require 'stringio'
require 'benchmark/ips'
require 'benchmark/memory'
str = "test\n" * 100
Benchmark.ips do |x|
x.report('regex') { str[/.*/] }
x.report('index') { str[0..(str.index("\n") || -1)] }
x.report('stringio') { StringIO.open(str, &:readline) }
x.report('each_line') { str.each_line.first.chomp }
x.report('lines') { str.lines.first }
x.report('split') { str.split("\n").first }
x.compare!
end
Benchmark.memory do |x|
x.report('regex') { str[/.*/] }
x.report('index') { str[0..(str.index("\n") || -1)] }
x.report('stringio') { StringIO.open(str, &:readline) }
x.report('each_line') { str.each_line.first.chomp }
x.report('lines') { str.lines.first }
x.report('split') { str.split("\n").first }
x.compare!
end
Comparison:
regex: 5099725.8 i/s
index: 4968096.7 i/s - 1.03x slower
stringio: 3001260.8 i/s - 1.70x slower
each_line: 2330869.5 i/s - 2.19x slower
lines: 187918.5 i/s - 27.14x slower
split: 182865.6 i/s - 27.89x slower
Comparison:
regex: 40 allocated
index: 120 allocated - 3.00x more
stringio: 120 allocated - 3.00x more
each_line: 208 allocated - 5.20x more
lines: 5064 allocated - 126.60x more
split: 5104 allocated - 127.60x more
您也可以使用truncate方法
“很久以前在一个遥远的世界”。截断(27,分隔符:“”)
#=>“很久以前,在
.truncate(200个字符,分隔符:“”)
<>不要在中间剪掉单词,在你选择的字符数之后找到一个空间。
+1因为这应该更快,尽管它要快一点。。。我敢说。。。Perl-ish,因为它会增加行噪波。:-)如果处理大量文本或长行,则可能比拆分或使用行
快得多。我建议您在答案中添加一些基准测试,以显示其优点。@铁皮人根据行
对其进行基准测试,无论字符串大小如何,都不会发现任何差异。事实证明,行
不返回数组,而是返回枚举数(更好!)。所以现在我认为@Victor Deryagin的答案是最好的。它不会比行
快,因为正如您所发现的,行
是一个枚举器。它比任何解决方案都要快得多,例如创建数组的split
,尤其是当生成的数组很大时。这对1.8.6中的项目很有用(不支持#行)。如果有很多行,则内存和性能都非常低效,由于需要分配每一行,请使用有助于完成回复的格式。你的格式真的不能做到这一点。谢谢@zhisme!