Javascript array join new line
From this question, this ... Show
is one way to split a string, but how do I join it back with newlines? Also, I wonder if I is say linux which uses whichever newline character, then switch to windows, won't my web app break? The newlines become not recognized? Or maybe the browser does some conversion?
asked Dec 1, 2010 at 3:54
Jiew MengJiew Meng 82.4k174 gold badges476 silver badges788 bronze badges If you want to join using newline characters, just do:
But if you want to display on the HTML page, you'd want to wrap each line in
answered Dec 1, 2010 at 3:59
David TangDavid Tang 90.4k29 gold badges165 silver badges149 bronze badges 2 You can use the
In CSS: remember to use
Syntle 5,0883 gold badges12 silver badges34 bronze badges answered Dec 1, 2010 at 3:56
Jacob RelkinJacob Relkin 158k32 gold badges341 silver badges318 bronze badges 2 Split it on /\r?\n/, in case the string includes the carriage returns with newlines. join it with '\n', in any browser and any os. answered Dec 1, 2010 at 4:23
1 As said, join is the best, but here is the hard way (untested, I hope it's not too trivial):
answered Dec 1, 2010 at 3:58
Eran MedanEran Medan 43.8k59 gold badges177 silver badges275 bronze badges Sometimes, while working with javascript arrays, there is a requirement to convert an array to a string with every array element in a newline. This article demonstrates how to convert an array to a string with newlines embedded in the javascript string. When
applied to the calling array, javascript’s join(separator) method will join all the array elements into a single string divided by the separator passed in the method’s argument. Example:- Convert the array [“Javascript”,
“Is”, “Popular”,”Language”] to string with every element of the array in a newline Code:- Advertisements Output:- Explanation:- Read More: I hope this javascript article helped you with conversions of the array to a string with all array elements into newline. Good Luck !!! How do I join a new line array?Convert array to string with newlines using join()
The join('\r\n') method is used. '\r\n' is passed in as the method's argument that acts as a separator while converting the array to string values. The array elements are divided into newline using '\r\n'.
Does \n work in JavaScript?The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.
What does .join do in JavaScript?join() The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.
What is RN in JavaScript?The RegExp \r Metacharacter in JavaScript is used to find the carriage return character (Carriage return means to return to the beginning of the current line without advancing downward).
|