如何检查字符串“ StartsWith”是否为另一个字符串?

人走茶凉 提交于 2020-08-06 16:15:48

问题:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!