Ruby - String chop and chomp


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.


s = "test\n\n\n"

puts s# from   w w w.  jav a  2 s. com

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


Related Topic