javascript - split string only on first specified character

JavaScript
[Edit]
+
0
-
0

JavaScript - split string only on first specified character

9600
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
const splitString = (text, delimiter) => { const index = text.indexOf(delimiter); if (index === -1) { return [text]; } return [ text.substring(0, index), text.substring(index + 1) ]; }; // Usage example: const text = 'a,b,c'; const parts = splitString(text, ','); console.log(parts); // ['a', 'b,c'] // See also: // // 1. Universal solution to split on N occurrences of delimiter // https://dirask.com/snippets/JavaScript-split-string-only-on-specified-delimited-by-number-of-occurrences-103zOj
Reset