How would I write the equivalent of C#'s String.StartsWith
in JavaScript?
(如何在JavaScript中编写等效于C#的String.StartsWith
?)
var haystack = 'hello world';
var needle = 'he';
haystack.startsWith(needle) == true
Note: This is an old question, and as pointed out in the comments ECMAScript 2015 (ES6) introduced the .startsWith
method.
(注意:这是一个古老的问题,正如注释中指出的那样,ECMAScript 2015(ES6)引入了.startsWith
方法。)
However, at the time of writing this update (2015) browser support is far from complete . (但是,在撰写此更新(2015)时, 浏览器支持还远远没有完成 。)
ask by translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…