

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Is it possible to have a function-based index in MySQL?
- Is it possible to select text boxes with JavaScript?
- 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 change the HTML content in JavaScript?
- Is it possible to write to MongoDB console in JavaScript execution?
- 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?
- Pull and add to set at the same time with MongoDB? Is it Possible?
- Is it possible to display substring from object entries in JavaScript?
- Is it possible to create a new data type in JavaScript?
- Why do Lua arrays (tables) start at 1 instead of 0?
- Is it possible to de-structure to already-declared variables? In JavaScript?
- Is love at first sight possible?
- Python program to return rows that have element at a specified index
Advertisements