Skip to content

Instantly share code, notes, and snippets.

@viniceosm
Created June 27, 2018 17:10
Show Gist options
  • Save viniceosm/e69564ba51c80e55850d5cf704634c51 to your computer and use it in GitHub Desktop.
Save viniceosm/e69564ba51c80e55850d5cf704634c51 to your computer and use it in GitHub Desktop.
split string passing start and end
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