How to get the first line of a multi-line string in JavaScript

1 Answer

0 votes
const multiLineString = 
        "First line\n" +
        "Second line\n" +
        "Third line\n" +
        "Fourth line";
         
const firstLine = multiLineString.substring(0, multiLineString.indexOf("\n"));
         
console.log("The first line is: " + firstLine);

 
 
  
/*
run
 
The first line is: First line
  
*/

 



answered Aug 13, 2024 by avibootz

Related questions

2 answers 145 views
1 answer 132 views
1 answer 124 views
1 answer 131 views
1 answer 114 views
1 answer 112 views
1 answer 133 views
...