ruby on rails中怎样去掉字符串结尾处的回车

Python013

ruby on rails中怎样去掉字符串结尾处的回车,第1张

ruby的String类有一个方法叫chomp,用来去掉字符串末尾的\n或\r

例子这样

"hello".chomp #=>"hello"

"hello\n".chomp #=>"hello"

"hello\r\n".chomp #=>"hello"

"hello\n\r".chomp #=>"hello\n"

"hello\r".chomp #=>"hello"

print "line 1: "line1 = STDIN.gets.chomp()

print "line 2: "line2 = STDIN.gets.chomp()

print "line 3: "line3 = STDIN.gets.chomp()

puts "I'm going to write these to the file."

target.write( [ line1 ,line2 ,line3 ,'' ].join( '\n ' ) )