From: Alan Munn Date: 2009-09-14T04:35:05+09:00 Subject: Re: normalizing newlines In article , 7stud -- wrote: > Alan Munn wrote: > > I have a script that processes text from the clipboard. Depending on the > > application the text is copied from, (even on the same platform) lines > > sometimes end in lf, cr, or possibly cr/lf. What's the best way of > > normalizing the lines so that my script will work independent of the > > actual newlines chosen? > > > > Look at the original strings in the examples: > > ----------------------------------------------------------- String#chomp > str.chomp(separator=$/) => new_str > ------------------------------------------------------------------------ > Returns a new +String+ with the given record separator removed from > the end of _str_ (if present). If +$/+ has not been changed from > the default Ruby record separator, then +chomp+ also removes > carriage return characters (that is it will remove +\n+, +\r+, and > +\r\n+). > > "hello".chomp #=> "hello" > "hello\n".chomp #=> "hello" > "hello\r\n".chomp #=> "hello" > "hello\n\r".chomp #=> "hello\n" > "hello\r".chomp #=> "hello" > "hello \n there".chomp #=> "hello \n there" > "hello".chomp("llo") #=> "he" Thanks. I'd prefer to normalize the lines and then use .split on \r thank chomp each line. Alan