12

I have a string like

This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day

I wanted to basically split this one long string at the commas and insert a new line so it becomes

This is great day
tomorrow is a better day
the day after is a better day
the day after the day after that is the greatest day

How can I do that ?

Andy
  • 18,723
  • 12
  • 46
  • 54

8 Answers8

43

With the built in split and join methods

var formattedString = yourString.split(",").join("\n")

If you'd like the newlines to be HTML line breaks that would be

var formattedString = yourString.split(",").join("<br />")

This makes the most sense to me since you're splitting it into lines and then joining them with the newline character.

Although I think speed is less important than readability in most cases, I was curious about it in this case so I've written a quick a benchmark.

It seems that (in chrome) using str.split(",").join("\n") is faster than str.replace(/,/g, '\n'); .

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
5

You could also replace them:

string.replace(/,/g, '\n');
Blender
  • 289,723
  • 53
  • 439
  • 496
1
<p id="errorMessage">Click | the |button |to |display| the| array| values| 
after| the| split.</p>

$(document).ready(function () {
var str = $("#errorMessage").text();
document.getElementById("errorMessage").innerHTML=str.split("|").join(" 
</br>"); }
MADHUR GUPTA
  • 1,014
  • 10
  • 14
  • Don't just blurt out code! Adding some text to describe how this best answers the question will improve the long-term value of the answer and help to prevent it from being deleted during review. Also, it may not be obvious to all readers how this solves the problem being that the split character provided by the OP was `,` rather than `|`. – NightOwl888 Apr 13 '18 at 03:53
0
> a = 'This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day'
> b = a.split(', ').join('\n')

"This is great day
tomorrow is a better day
the day after is a better day
the day after the day after that is the greatest day"
imkost
  • 8,033
  • 7
  • 29
  • 47
0

You can use .split() to create an array of all the parts of the string...

var str = 'This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day';

str.split(',');
  -> ["This is great day", " tomorrow is a better day", " the day after is a better day", " the day after the day after that is the greatest day"]

Now, you can do whatever you want with the different parts. Since you want to join with a new line, you can use .join() to put it back together...

str.split(',').join('\n');
  -> "This is great day
      tomorrow is a better day
      the day after is a better day
      the day after the day after that is the greatest day"
Hristo
  • 45,559
  • 65
  • 163
  • 230
0
TermsAndConditions = "Right to make changes to the agreement.,Copyright and intellectual property.,Governing law.,Warrantaay disclaimer.,Limitation of liability."
const TAndCList = this.invoiceTermsAndConditions.split(",").join("\n \n• ");

Output :

• Right to make changes to the agreement.
• Copyright and intellectual property.
• Governing law.
• Warrantaay disclaimer.
• Limitation of liability.
Sushil
  • 670
  • 7
  • 14
0

I will suggest split the array on commas and then use the for loop. below code snippet is for vue.js

             <div 
             v-for="(arrayElement) in Array"
             :key="`Array-${arrayElement.id}`"
             class="column">
                {{ arrayElement.name }}
             </div> 
-1

Without testing on my browser try:

var MyStr="This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day";
Var splitedStr = MyStr.split(",");

var returnStr = '';
for (var i = 0; i < splitedStr.length; i++)
{
    returnStr += splitedStr[i] + '<br />';
}

document.write(returnStr);
AkisC
  • 817
  • 2
  • 12
  • 27