Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby .split('\n') not splitting on new line

Tags:

string

ruby

Why does this string not split on each "\n"? (RUBY)

"ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t".split('\n') >> ["ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t"] 
like image 966
user2012677 Avatar asked May 06 '13 20:05

user2012677


People also ask

How do you split text into a new line?

To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings.

How do you break a line in Ruby?

\r\n should probably do the trick.

How do you make a new line in Ruby?

"\n" is newline, '\n\ is literally backslash and n.

How do you delete a new line character in Ruby?

The chomp function helps remove the trailing newline character ie '\n', from any string. Note that it is just one among the dozen odd such string methods that Ruby ships with.


1 Answers

You need .split("\n"). String interpolation is needed to properly interpret the new line, and double quotes are one way to do that.

like image 97
Mori Avatar answered Oct 21 '22 06:10

Mori