How to split a string into an array by delimiter and remove empty elements in JavaScript

1 Answer

0 votes
function splitString(s, delimiter) {
    return s.split(delimiter).filter(element => element !== "");
}

function main() {
    const s = "C#,JavaScript,,C,,,Python,,,,,C++,,";
    const arr = splitString(s, ",");

    arr.forEach(element => console.log(element));
}

main();



/*
run:

C#
JavaScript
C
Python
C++

*/

 



answered Sep 23, 2024 by avibootz
edited Aug 28 by avibootz

Related questions

...