Ruby - String chop and chomp

Introduction

The chop and chomp methods can remove characters from the end of a string.

The chop method returns a string with the last character removed or with the carriage return and newline characters removed (\r\n) if these are found at the end of the string.

The chomp method returns a string with the terminating carriage return or newline character removed or both the carriage return and the newline character if both are found.

Demo

s = "test\n\n\n"

puts s#   w  w w  .j av  a  2s.  c o  m

puts ">"+ s.chomp + "<"

Result

Related Topic