
- 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
Is it possible to have JavaScript split() start at index 1?
As of the official String.prototype.split() method there exist no way to start splitting a string from index 1 or for general from any index n, but with a little tweak in the way we use split(), we can achieve this functionality.
We followed the following approach −
We will create two arrays −
- One that is splitted from 0 to end --- ACTUAL
- Second that is splitted from 0 TO STARTPOSITION --- LEFTOVER
Now, we iterate over each element of leftover and splice it from the actual array. Thus, the actual array hypothetically gets splitted from STARTINDEX to END.
Example
const string = 'The quick brown fox jumped over the wall'; const returnSplittedArray = (str, startPosition, seperator=" ") => { const leftOver = str.split(seperator, startPosition); const actual = str.split(seperator); leftOver.forEach(left => { actual.splice(actual.indexOf(left), 1); }) return actual; } console.log(returnSplittedArray(string, 5, " "));
Output
["over", "the", "wall"]
- Related Articles
- Is it possible to have a function-based index in MySQL?
- Is it possible to select text boxes with JavaScript?
- Is it possible to change the HTML content in JavaScript?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to write data to file using only JavaScript?
- Possible to split a string with separator after every word in JavaScript
- Is it possible that there is a predecessor of 1?
- Is it possible to have View and table with the same name in MySQL?
- Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?
- Is it possible to display substring from object entries in JavaScript?
- Is it possible to create a new data type in JavaScript?
- Is it possible to de-structure to already-declared variables? In JavaScript?
- Is it possible to have an HTML canvas element in the background of my page?
- Is it possible to have multiple try blocks with only one catch block in java?
- Python program to return rows that have element at a specified index

Advertisements