I have a long string of ISO dates:
var str = \"\'2012-11-10T00:00:00.000Z\', \'2012-11-11T00:00:00.000Z\', **** \'2013-11-12T00:00:00.000Z\'\";
If you're really getting a performance problem from the large array (dont' optimize prematurely), you could use slice to extract the single strings and indexOf/lastIndexOf to find their positions:
str.slice(0, str.indexOf(','))
and
str.slice(str.lastIndexOf(',')+1) // 1==','.length
There is jsperf
using substr
/ substring
that is the most optimal way. See:
https://jsperf.com/slice-vs-substr-vs-substring-vs-split-vs-regexp
It's really not a waste of space, since Javascript is barely taking up any memory on the computer. Unless the string is gigabytes long, I wouldn't worry about it. To get the first and last, just do this:
arr=str.split(',');
var first=arr.shift(); //or arr[arr.length-1];
var last=arr.pop(); //or arr[0];
I have a long string of ISO dates
Assuming these are all using timezone Z
, year >= 0
and are spaced and quoted the same way, you don't even have to search for ,
because they will be of the same length.
var first = str.slice(1, 25),
last = str.slice(-25, -1);