问题:
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)时, 浏览器支持还远远没有完成 。
解决方案:
参考一: https://stackoom.com/question/2iDU/如何检查字符串-StartsWith-是否为另一个字符串参考二: https://oldbug.net/q/2iDU/How-to-check-if-a-string-StartsWith-another-string
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4475771