Logo

Programming-Idioms

History of Idiom 96 > diff from v23 to v24

Edit summary for version 24 by programming-idioms.org:
New JS implementation by user [programming-idioms.org]

Version 23

2016-05-03, 20:51:05

Version 24

2016-05-03, 20:56:51

Idiom #96 Check string prefix

Set boolean b to true if string s starts with prefix prefix, false otherwise.

Idiom #96 Check string prefix

Set boolean b to true if string s starts with prefix prefix, false otherwise.

Code
var b = (s.lastIndexOf(prefix, 0) === 0);
Comments bubble
Note the second parameter to lastIndexOf. This is not, however, the most readable possible code.
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
Origin
http://stackoverflow.com/a/4579228/871134