字符串笔记

indexOf

从字符串中查找指定字符串,如果能够在字符串中查找到结果,则返回指定字符串所在位置,如果找不到,返回-1

let str = 'welcome to my notepad';
console.log(str.indexOf('to'));
//返回结果
8

let str = 'welcome to my notepad';
console.log(str.indexOf('hello'));
//返回结果
-1
include

从字符串中查找指定字符串,如果有结果返回true,没有结果返回false

let str = 'welcome to my notepad';
console.log(str.includes('to'));
//返回结果
true
//判断浏览器
console.log(navigator.userAgent.includes('Chrome'))
startsWith

判断某字符串是否是以指定字符串开头

let str = 'http://www.baidu.com/';
console.log(str.startsWith('http'));
//返回结果
true
endsWith

判断某字符串是否是以指定字符串结尾

let str = 'http://www.baidu.com/img/bd_logo1.png';
console.log(str.endsWith('.png'));
//返回结果
true
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容