viniceosm
6/27/2018 - 5:10 PM

split string passing start and end

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']