
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Split string into equal parts JavaScript
We are required to write a JavaScript function that takes in a string and a number n as two arguments (the number should be such that it exactly divides the length of string). And we have to return an array of n strings of equal length.
For example −
If the string is "helloo" and the number is 3 Our output should be: ["ho", "eo", "ll"]
Here, each substring exactly contains (length of array/n) characters. And each substring is formed by taking corresponding first and last letters of the string alternatively
Let's write the code for this function −
Example
const str = 'helloo'; const splitEqual = (str, n) => { if(str.length % n !== 0){ return false; } const len = str.length / n; const strArray = str.split(""); const arr = []; let i = 0, char; while(strArray.length){ if(i % 2 === 0){ char = strArray.shift(); }else{ char = strArray.pop(); }; if(i % len === 0){ arr[i / len] = char; }else{ arr[Math.floor(i / len)] += char; }; i++; }; return arr; }; console.log(splitEqual(str, 3));
Output
The output in the console will be −
[ 'ho', 'eo', 'll' ]
- Related Articles
- Divide a string into n equal parts - JavaScript
- Split a string in equal parts (grouper in Python)
- Split string into groups - JavaScript
- Split the array into equal sum parts according to given conditions in C++
- Splitting a string into parts in JavaScript
- Splitting a string into maximum parts in JavaScript
- How can we split the name string into two parts by using MySQL SUBSTRING_INDEX() function?
- How can we split the name string into three parts by using MySQL SUBSTRING_INDEX() function?
- How to break or split address into separated parts in Excel?
- How to split a long string into a vector of substrings of equal sizes in R?
- How to split a data frame in R into multiple parts randomly?
- Partition Array Into Three Parts With Equal Sum in Python
- Split string into sentences using regex in PHP
- Python – Split Numeric String into K digit integers
- Split number into n length array - JavaScript

Advertisements