“I have a string that is read from a text file but in Ubuntu Linux, and I try to delete its newline character from the end. I used all the ways. But for s/\n|\r/-/ (I look whether it finds any replaces any new line string) it replaces the string but it still goes to next line when I print it. Moreover, when I used chomp orchop string is completely deleted. I could not find any other solution. Do you have any idea? ”

大体意思就是文本最后包含Unicode字符集中的换行符,但是使用chomp无法解决。

解决方案:

     1. use 5.010;  

      The correct way to remove Unicode linebreak graphemes, including CRLF pairs, is using the \R regex metacharacter, introduced in v5.10.The use encoding pragma is strongly deprecated. You should either use the use open pragma, or use an encoding in the mode argument on 3-arg open, or use binmode.

      $data=~s/\R//g;

      2. 直接替换  $data=~s/[\n\r]+$//;