ES6 字符串方法includes(), startsWith(), endsW

ES6 字符串方法includes(), startsWith(), endsWith()详解

语法:

includes(searchvalue, start):返回布尔值,表示是否找到了参数字符串。

startsWith(searchvalue, start):返回布尔值,表示参数字符串是否在原字符串的头部。

endsWith(searchvalue, start):返回布尔值,表示参数字符串是否在原字符串的尾部。



参数说明:

searchvalue 必需,要查找的字符串。

start 可选,查找的开始位置,默认为 0。



返回值:

Boolean 如果字符串是以指定的子字符串开头返回 true,否则 false。




示例:

javascript中字符串处理并没有 StartWith 和 EndWith 这俩个方法,这里说的是手动构建这俩个方法.

JQuery 也是没有这俩个方法的,而是利用其丰富的选择器来达到此效果.


首选javascript下着俩个函数的构建如下:


应用方式如下:

var url = location.href;

if (url.startWith('http://www.baidu.com'))

{

    //如果当前url是以 http://www.baidu.com 开头(以下是处理代码)

}

你可能感兴趣的:(ES6 字符串方法includes(), startsWith(), endsW)