ruby/doc/string/chop.rdoc
2025-07-09 13:38:53 -04:00

19 lines
662 B
Text

Returns a new string copied from +self+, with trailing characters possibly removed.
Removes <tt>"\r\n"</tt> if those are the last two characters.
"abc\r\n".chop # => "abc"
"тест\r\n".chop # => "тест"
"こんにちは\r\n".chop # => "こんにちは"
Otherwise removes the last character if it exists.
'abcd'.chop # => "abc"
'тест'.chop # => "тес"
'こんにちは'.chop # => "こんにち"
''.chop # => ""
If you only need to remove the newline separator at the end of the string,
String#chomp is a better alternative.
Related: see {Converting to New String}[rdoc-ref:String@Converting+to+New+String].