Partition a string into chunks of given size in JavaScript
This post will discuss how to partition a string into chunks of given size in JavaScript.
To partition a string into chunks of given size in JavaScript, there are several approaches we can take. Here are three possible solutions:
1. Using a loop and slicing
This is a simple and straightforward way to iterate over the string using a for loop and use the slice() or substring() function to extract a substring of the desired length at each iteration. The slice() function returns a part of the string from a start index to an end index. We can use a for loop to increment the start and end indexes by the chunk size, and push the substrings to an array. For example, if we want to partition a string into chunks of 3 characters, we can use this function:
|
1 2 3 4 5 6 7 8 9 10 11 |
function partition(str, chunkSize) { let chunks = []; for (let i = 0; i < str.length; i += chunkSize) { chunks.push(str.slice(i, i + chunkSize)); } return chunks; } let str = "Hello world!"; let result = partition(str, 3); console.log(result); // ["Hel", "lo ", "wor", "ld!"] |
2. Using String.match() function
This is a more concise and elegant way to partition a string into chunks of the given size using a regular expression that matches any character (.) exactly n times ({n}), where n is the chunk size. We can use the match() function to get an array of the matched substrings. For example, if we want to partition a string into chunks of 3 characters, we can use str.match(/.{1,3}/g). This will return an array of substrings, or null if the string is empty.
|
1 2 3 4 5 6 7 8 |
function partition(str, chunkSize) { let regex = new RegExp(`.{1,{chunkSize}}`, 'g'); return str.match(regex); } let str = "Hello world!"; let result = partition(str, 3); console.log(result); // ["Hel", "lo ", "wor", "ld!"] |
3. Using recursion and slicing
We can also use a recursive function to partition a string into chunks of a fixed size. Recursion can be used to divide a problem into smaller subproblems that are easier to solve. The idea is to use the slice() function to get the first chunk of the string, and then call the function again with the remaining part of the string. The base case is when the string is empty or shorter than the chunk size. For example, if we want to partition the string "Hello world!" into chunks of size 3, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
function partition(str, chunkSize) { // Base case: if the string is empty or shorter than the chunk size, return it as an array if (str.length <= chunkSize) { return [str]; } // Recursive case: get the first chunk and concatenate it with the recursive call on the rest of the string return [str.slice(0, chunkSize)].concat(partition(str.slice(chunkSize), chunkSize)); } let str = "Hello world!"; let result = partition(str, 3); console.log(result); // ["Hel", "lo ", "wor", "ld!"] |
That’s all about partitioning a string into chunks of given size in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)