Created
June 27, 2018 17:10
-
-
Save viniceosm/e69564ba51c80e55850d5cf704634c51 to your computer and use it in GitHub Desktop.
split string passing start and end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.splitInterval = function (delimiter, start = 0, end) { | |
return this.split(delimiter).filter(function(_, i) { | |
if (end === undefined) { | |
return i >= start; | |
} | |
return i >= start && i <= end; | |
}); | |
} | |
var str = '1171_1_False_False'; | |
// Starts at index 1 to 2 | |
str.splitInterval('_', 0, 1); // ['1', 'False'] | |
// Starts at index 1 until the end | |
str.splitInterval('_', 1); // ['1', 'False', 'False'] | |
// Equal split | |
str.splitInterval('_'); // ['1171', '1', 'False', 'False'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment