JavaScript - Can I convert multiple string lines to single line?

Paul125
Kilo Guru

Hi - I want to convert below lines to single line. Is this something possible with JavaScript?

This is line one
This is line two
             This is a broken line
This is line three

Expected output:

This is line one, This is line two This is a broken line, This is line three
             
1 ACCEPTED SOLUTION

Try this out

var line = '';
var lines = multiLines.split("\n"); //multiLines contains your text
for(var i=0; i<lines.length; i++){
  if(lines[i].startsWith(" ")){
    line += " " + lines[i].trim();  
  }else{
    line += "," + lines[i].trim();  
      
  }    
}

line = line.substring(1).trim();

View solution in original post

6 REPLIES 6

Paul125
Kilo Guru

Great! That works.

Sanjay Bagri1
Tera Guru

Hi

Please see this thread for that :

https://stackoverflow.com/questions/32309670/change-multi-line-strings-to-single-line

 

Please mark as correct if it is helpful for you .

Thanks

Sanjay Bagri

Dxsherpa.com