xxxxxxxxxx
const splitAt = (index: number) => (x: string) => [x.slice(0, index), x.slice(index)]
xxxxxxxxxx
function splitStringAtIndex(str, index) {
// Make sure the index is within the string length
if (index < 0 || index >= str.length) {
return "Invalid index";
}
// Split the string at the given index
const firstPart = str.slice(0, index);
const secondPart = str.slice(index);
return [firstPart, secondPart];
}
// Example usage
const str = "Hello, World!";
const index = 5;
const result = splitStringAtIndex(str, index);
console.log(result); // Output: ["Hello", ", World!"]