Writing ECMAScript 6's proposed .contains
, .startsWith
, and .endsWith
methods using good ole .indexOf
.
'carlos'.contains('car');
// true
'carlos'.indexOf('car') >= 0;
// true
'carlos'.startsWith('car');
// true
'carlos'.indexOf('car') === 0;
// true
This one looks a little dirty to me because I needed to know the fname
and arg
in advance. Is there a way to do it so that I don't need those two variables?
'carlos'.endsWith('los');
// true
var fname = 'carlos';
var arg = 'los';
fname.indexOf(arg) == fname.length - arg.length;
// true
:+1