需求:
js字符串(String)方法汇总
索引:
1,str.concat()
2,str.endsWith()
3,str.includes()
4,str.indexOf()
5,str.lastIndexOf()
6,str.replace()
7,str.slice()
8,str.split()
9,str.toLowerCase()
10,str.toUpperCase()
11,str.match()
12,str.trim()
13,str.search()
14,str.charCodeAt()
15,String.fromCharCode()
正文
1,str.concat()
(1)作用:将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回;
(2)语法:str.concat(str1,str2,str3…);
(3)语法解释:str,str1,str2,str3指代字符串;和+ +=操作符作用一样,建议优先使用+ +=;
(4)返回值:返回拼接后的字符串;
(5)示例代码:
const str='hello';
const res=str.concat(' world','!');
console.log(res);// hellow world!
2,str.endsWith()
(1)作用:判断当前字符串是否是以另外一个给定的子字符串“结尾”的;
(2)语法:str.endsWith(searchStr);
(3)语法解释:str被检索的原字符串,searchStr用来搜索的字符串;该方法大小写敏感;
(4)返回值:返回布尔值;
(5)示例代码:
const str='hello world!';
const res=str.endsWith('world!');
console.log(res);// true
const res1=str.endsWith('WORLD!');
console.log(res1);// false
3,str.includes()
(1)作用:判断一个字符串是否包含在另一个字符串中;
(2)语法:str.includes(searchStr);
(3)语法解释:str被检索的原字符串,searchStr用来搜索的字符串;该方法大小写敏感;
(4)返回值:返回布尔值;
(5)示例代码
const str='hello world!';
const res=str.includes('hello');
console.log(res);// true
const res1=str.includes('HELLO');
console.log(res1); // false
4,str.indexOf()
(1)作用:返回调用 String 对象中第一次出现的指定值的索引值