Try this
function splitValue(value, index) {
return value.substring(0, index) + "," + value.substring(index);
}
console.log(splitValue("3123124", 2));
Answer from Chamika Sandamal on Stack Overflow Top answer 1 of 11
150
Try this
function splitValue(value, index) {
return value.substring(0, index) + "," + value.substring(index);
}
console.log(splitValue("3123124", 2));
2 of 11
83
ES6 1-liner
// :: splitAt = (number, any[] | string) => [any[] | string, any[] | string]
const splitAt = (index, xs) => [xs.slice(0, index), xs.slice(index)]
console.log(
splitAt(1, 'foo'), // ["f", "oo"]
splitAt(2, [1, 2, 3, 4]) // [[1, 2], [3, 4]]
)
TypeScript:
export function splitAt<T>(index: number, xs: T[]): [T[], T[]];
export function splitAt(index: number, xs: string): [string, string];
export function splitAt<T>(index: number, xs: T[] | string): [T[] | string, T[] | string] {
return [xs.slice(0, index), xs.slice(index)];
}
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › split
String.prototype.split() - JavaScript | MDN
The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
Split string into individual characters?
Split string into individual characters · You have passed it a string with a space in it - that is you telling it to break it up using the spaces as delimiters. If you send it nothing, it will just break it into every character, split(); · The split() method divides a String into an ordered ... More on forum.freecodecamp.org
Split String at Index?
I’m basically wondering if I could do something like this: string.split(string,index) This doesn’t work but it doesn’t error, it just doesn’t split anything. Does anyone know something like this that would work? Or do … More on devforum.roblox.com
Efficient way to split string at specific index
I’m currently trying to parse a file whose data lines are composed of 8 character columns without delimiter. Such as 11111111222222223333333344444444555555556666666677777777888888889999999911111111 So far, I’m working with indices and comprehension to do that with split8_1(str::String) ... More on discourse.julialang.org
How to predict what index part of a string will split too based only on an index in the larger string?
I don't really understand what you're asking or what problem you're trying to solve?
Have you considered something like a rope data structure? I've heard that's used in text editors.
More on reddit.comVideos
W3Schools
w3schools.com › jsref › jsref_split.asp
JavaScript String split() Method
The split() method splits a string into an array of substrings.
DigitalOcean
digitalocean.com › community › tutorials › how-to-index-split-and-manipulate-strings-in-javascript
How To Index, Split, and Manipulate Strings in JavaScript | DigitalOcean
August 24, 2021 - Now that we have a new array in ... with an index number. ... If an empty parameter is given, split() will create a comma-separated array with each character in the string. By splitting strings you can determine how many words are in a sentence, and use the method as a way to determine people’s first names and last names, for example. The JavaScript trim() method ...
SitePoint
sitepoint.com › blog › javascript › quick tip: how to split a string into substrings in javascript
How to Split a String into Substrings in JavaScript — SitePoint
November 6, 2024 - In this example, you retrieve the index of the hash character # in the variable url. If the value of the index is not -1, you retrieve the substring from url starting at the index of the hash. You can try it out in the following CodePen demo. See the Pen Using substring with indexOf to split string by SitePoint (@SitePoint) on CodePen.
Medium
medium.com › nerd-for-tech › basics-of-javascript-string-split-method-4647aa1b0f4e
Basics of Javascript · String · split() (method) | by Jakub Korch | Nerd For Tech | Medium
June 20, 2021 - You can see that happening in case of separator being regular expression, where we are searching for capital letter optionally preceded by empty space. Capital H at the beginning of the string fits this condition, so the split creates empty space as a first element of the resulting array.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › slice
String.prototype.slice() - JavaScript | MDN
The slice() method of String values extracts a section of this string and returns it as a new string, without modifying the original string. const str = "The quick brown fox jumps over the lazy dog."; console.log(str.slice(31)); // Expected output: "the lazy dog." console.log(str.slice(4, 19)); ...
ReqBin
reqbin.com › code › javascript › wyqvcilw › javascript-split-string-example
How do I split a string in JavaScript?
To split a string in JavaScript, you can use the string.split(separator, limit) method. The split() method splits the given string into an array of strings using "separator" as the delimiter.
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-split-method
JavaScript String split() Method - GeeksforGeeks
The JavaScript split() method is used to break a string into an array of substrings based on a given separator.
Published January 16, 2026
Flexiple
flexiple.com › javascript › javascript-split-string
JavaScript split string - String into substrings | Flexiple tutorial | JavaScript - Flexiple
March 11, 2022 - If a separator is not passed, the array will contain one element containing the entire string. let Flexiple = 'Hire top freelance developers' let week = 'Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday' let flexiplelist = Flexiple.split(" ") let weeklist = week.split(",") let flexiplelist2 = Flexiple.split() let weeklist2 = week.split(",",3) console.log(weeklist) console.log(flexiplelist) console.log(flexiplelist2) console.log(weeklist2)
Programiz
programiz.com › javascript › library › string › split
JavaScript String split()
Returns an Array of strings, split at each point where the separator occurs in the given string. Note: The split() method does not change the original string. console.log("ABCDEF".split("")); // [ 'A', 'B', 'C', 'D', 'E', 'F' ] const text = "Java is awesome.
JavaScript in Plain English
javascript.plainenglish.io › how-to-split-a-string-once-in-javascript-3c3cfefe1321
How do you split a string once in JavaScript? | by John Au-Yeung | JavaScript in Plain English
June 10, 2024 - In this article, we’ll look at how to split a string once in our JavaScript code. To split a string once in our JavaScript code, we can use the JavaScript string’s indexOf method with the JavaScript string’s slice method to find the index of the first instance of the separator and then do the slicing.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.