How to split a string by newline in TypeScript

1 Answer

0 votes
const str = "TypeScript\nPython\n\r\nC++\nC";

const arr = str.split(/\r?\n/).filter(element => element);
 
console.log(arr);
 
 
 
 
/*
run:
 
["TypeScript", "Python", "C++", "C"] 
 
*/

 



answered May 1, 2022 by avibootz
...