Sometimes, we want to split and modify a string in Node.js and JavaScript.
In this article, we’ll look at how to split and modify a string in Node.js and JavaScript.
How to split and modify a string in Node.js and JavaScript?
To split and modify a string in Node.js and JavaScript, we use the split
and map
methods.
For instance, we write
const str = "123, 124, 234,252";
const arr = str.split(",").map((val) => Number(val) + 1);
console.log(arr);
to call str.split
to split the string into a string array by the comma.
Then we call map
with a callback to map each value to the value we want.
Conclusion
To split and modify a string in Node.js and JavaScript, we use the split
and map
methods.