How can I split a string separated by return #js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I split a string separated by return #js

I want to split sting which are separated by a single return as shown below: Apple Orange Banana

14th Jun 2017, 6:08 AM
Ishu Gupta
1 Answer
+ 4
Try to do: var stringarray = multilinetext.split('\n'); It will assign each line of your multilines string: stringarray == ['Apple','Orange','Banana'] from your example... since you're in an OS using '\n' as line separator. Else try the combinations of '\r\n' which is mostly used on Windows systems ^^: the only difference in the first case will be an eventual '\r' invisible character remaining at end of each line splitted, since in the second case, lines will not be splitted... You can even do: alltext = alltext.replace(/\r/g,''); // retrieve all evetuals '\r' char present; var lines = alltext.split('\n'); // now works right in all cases
14th Jun 2017, 9:17 AM
visph
visph - avatar