Split string with commas to new line

前端 未结 6 2191
终归单人心
终归单人心 2021-02-04 19:11

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
         


        
6条回答
  •  天命终不由人
    2021-02-04 19:33

    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"
    

提交回复
热议问题