JavaScript Program to Split a String into a Number of Sub-Strings Last Updated : 08 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Given a string, the task is to split the string into number of sub-strings using JavaScript. This task is common in scenarios where data needs to be segmented or processed in chunks. This article provides a flexible solution that can divide a given string into a desired number of smaller substrings, allowing for efficient manipulation or analysis of the data. Table of Content Using substring() MethodUsing slice() MethodSplit a String into Number of Sub-Strings using substring() MethodThis approach divides the length of the string by the specified number of substrings to determine the approximate length of each substring. It then iteratively extracts substrings of equal length using the substring() method until the entire string is segmented. JavaScript function splitString(str, splitCount) { const substrings = []; const substringLength = Math.ceil(str.length / splitCount); let startIndex = 0; for (let i = 0; i < splitCount; i++) { substrings.push(str.substring(startIndex, startIndex + substringLength)); startIndex += substringLength; } return substrings; } // Driver code console.log(splitString("hello world", 3)); Output[ 'hell', 'o wo', 'rld' ] Split a String into Number of Sub-Strings using slice() MethodThis method divides the length of the string by the specified number of substrings to determine the approximate length of each substring. It then utilizes the slice() method to extract substrings of equal length iteratively until the entire string is segmented Example: JavaScript function splitString(str, splitCount) { const substrings = []; const substringLength = Math.ceil(str.length / splitCount); let startIndex = 0; for (let i = 0; i < splitCount; i++) { substrings.push(str.slice(startIndex, startIndex + substringLength)); startIndex += substringLength; } return substrings; } // Driver code console.log(splitString("hello world", 3)); Output[ 'hell', 'o wo', 'rld' ] Comment More infoAdvertise with us Next Article JavaScript Program to Split a String into a Number of Sub-Strings A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions JavaScript-String-Questions Similar Reads How To Split a String Into Segments Of n Characters in JavaScript? When working with JavaScript, you might encounter a situation where you need to break a string into smaller segments of equal lengths. This could be useful when formatting a serial number, creating chunks of data for processing, or splitting a long string into manageable pieces. In this article, we 6 min read JavaScript String split() Method The JavaScript split() method divides a string into an array of substrings based on a specified separator, such as a character, string, or regular expression. It returns the array of split substrings, allowing manipulation of the original string data in various ways.JavaScriptlet str = "Hello and We 3 min read Shell Script to Split a String Shell Scripting or Shell Programming is just like any other programming language. A shell is a special program that provides an interface between the user and the operating system. In Linux/Unix the default shell used is bash and in windows, it is cmd(command prompt). We use the terminal to run a sh 3 min read C# - Different Ways to Find All Substrings in a String Given a string as an input we need to find all the substrings present in the given string. Example: Input: geeks Output: g e e k s ge ee ek ks gee eek eks geek eeks geeks Input: ab Output: a b abMethod 1: Using Substring() method We can find all the substrings from the given string using the Substri 3 min read Bash Scripting - Split String In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various met 4 min read Like