The time limit that .split () splits rather than slices the resulting array

Indeed, pretty much what the name says.

Say you have this line:

var theString = "a=b=c=d"; 

Now, when you run theString.split("=") , the result will be ["a", "b", "c", "d"] , as expected. And of course, when you run theString.split("=", 2) you get ["a", "b"] , which after reading the MDN page for String#split() makes sense to me.

However, the behavior I'm looking for is more like Java String#split() : instead of just building an array that returns the first n elements, it builds an array of the first n-1 matches, then adds all the remaining characters as the last element of the array. See related documents for a better description.

How can I get this effect in javascript?

I am looking for an answer with better performance that works like a Java implementation, although the actual way it works may be different.

I would post my attempt, but I do not know how to write about it at all.

+8
source share
7 answers

If you need the exact equivalent of a Java implementation (without error checking or security suggestions, etc.):

 function split(str, sep, n) { var out = []; while(n--) out.push(str.slice(sep.lastIndex, sep.exec(str).index)); out.push(str.slice(sep.lastIndex)); return out; } console.log(split("a=b=c=d", /=/g, 2)); // ['a', 'b', 'c=d'] 

This has the added benefit that you did not calculate the full split in advance, as you mentioned in your question.

+6
source

I would use something like this:

 function JavaSplit(string,separator,n) { var split = string.split(separator); if (split.length <= n) return split; var out = split.slice(0,n-1); out.push(split.slice(n-1).join(separator)); return out; } 

What we do here:

  • Full line split
  • Taking the first n-1 elements as described.
  • Re-merge the remaining elements.
  • Adding them to the array from step 2 and returning.

You might think that you can bind all these calls together, but .push() mutates the array, rather than returning a new one. It is also a little easier for you to follow this path.

+3
source

Another possible implementation:

 function split(s, separator, limit) { // split the initial string using limit var arr = s.split(separator, limit); // get the rest of the string... var left = s.substring(arr.join(separator).length + separator.length); // and append it to the array arr.push(left); return arr; } 

Fiddle is here .

+2
source

If you want to do this in fewer lines and avoiding loops:

 const theString = "some=string=with=separators"; const limit = 2; const parts = theString.split('=', limit); parts.push(theString.slice(parts.join('').length + limit)); 
+2
source

Are you looking for something closer to PHP explode ?

Here is the method I developed:

 String.prototype.explode = function(sep, n) { var arr = this.split(sep, n) if (arr[n-1] != undefined) arr[n-1] += this.substring(arr.join(' ').length); return arr; } 

This method breaks the line, as usual, determines whether our limit is successful and uses substring to add text beyond the limits of our last partition (we can directly access the offset of the first character after the last section, getting the length of join used in an array with any single symbol as a separator)

This method is used the same way as split :

 str = 'my/uri/needs/to/be/split'; splitResult = str.split('/', 4); explodeResult = str.explode('/', 4); console.log(splitResult); console.log(explodeResult); // The following will be written to the console: // splitResult: ["my", "uri", "needs", "to"] // explodeResult: ["my", "uri", "needs", "to/be/split"] 

And of course, this can also be minimized as a function:

 function explode(str, sep, n) { var arr = str.split(sep, n) if (arr[n-1] != undefined) arr[n-1] += this.substring(arr.join(' ').length); return arr; } str = 'my/uri/needs/to/be/split'; explodeResult = explode(str, '/', 4); 
0
source

 const theString = "a=b=c=d"; const [first, ...rest] = theString.split("="); const second = rest.join("=") console.log(first, second) 

If you are using ECMA 2015, you just need 2 lines.

0
source

This is my implementation:

 String.prototype.splitRemainder = function(delim, count) { if (typeof delim !== 'string') { return this.split(); } if (typeof count !== 'number') { return this.split(delim); } if (count < 2) { return this.split(delim); } count--; const parts = this.split(delim, count); const remainder = this.slice(parts.join('').length + count); if (remainder.length > 0) { parts.push(remainder); } return parts; } console.log("dasd asds asds asdasd asdasdas".splitRemainder(" ", 4)); console.log("hello-to-you-too".splitRemainder("-",2)); 

Please note that this is not the most efficient way to implement it. So if you are looking for the most effective solution, it is not.

0
source

All Articles