目录
2、charCodeAt():返回指定位置字符的Unicode编码
10、toUpperCase(),toLocaleUpperCase():将字符串转换成大写
11、toLowerCase(),toLocaleLowerCase():将字符串转换成小写
12、substr():从起始索引号提取字符串中指定数目的字符
13、substring():提取字符串中两个指定索引号之间的字符
1、charAt():返回指定位置的字符
const str = "ahduafghasfsd" console.log(str.charAt(2));//d
假如传入两个参数,则第二个参数是无效的
const str = "ahduafghasfsd" console.log(str.charAt(2,10));//d
2、charCodeAt():返回指定位置字符的Unicode编码
const str = "ahduafghasfsd" console.log(str.charCodeAt(2));//100
3、concat():连接字符串
const str1 = "hello" const str2 = " world" const str3 = str1.concat(str2) console.log(str3);//hello world
4、 indexOf(): 检索字符串
返回的是字符在字符串的第一个下标,如果字符串中没有该字符,则返回-1
const str = "hello" console.log(str.indexOf("l"));//2 console.log(str.indexOf("b"));//-1
5、match():匹配值或正则
在字符串内检索指定的值或找到一个或多个正则表达式的匹配,返回的是值而不是值的位置,若没有则返回null。
正则表达式不熟的可以查询网页看看,此处的意思是全局下匹配字母a-z
const str = "hello" console.log(str.match("l"));//["l", index: 2, input: "hello", groups: undefined] console.log(str.match("b"));//null console.log(str.match(/[a-z]/g));//["h", "e", "l", "l", "o"]
6、replace():替换匹配的字符串
两个参数,第一个是要替换的参数,第二个是替换的参数
const str = "hello" console.log(str.replace(str, "world"));//world
7、search():检索与字符串匹配的子串
返回的是下标
const str = "hello" console.log(str.search("ll"));//2
可以发现的是,search()和indexOf()很像,但其实他们还是有区别的
首先要明确search()的参数必须是正则表达式,而indexOf()的参数只是普通的字符串。indexOf()是比search()更加底层的方法。
如果只是一个具体字符串来检索,那么使用indexOf()的系统资源消耗更小,效率更高;如果查找具有某些特征的字符串(例如查找以a开头,后面是数字的字符串),那么indexOf()就无能为力,必须要使用正则表达式和search()方法了。
大多是时候用indexOf()不是为了真的想知道子字符串的位置,而是想知道长字符串中有没有包含这个子字符串。若果返回索引为-1,那么说明没有,反之则有。
const str = "11hello" console.log(str.indexOf("ll")); //2 console.log(str.search("ll")); //2 console.log(str.indexOf(/[a-z]/g)); //-1 console.log(str.search(/[a-z]/g)); //2
上面是一个简单的一个小例子,大家可以看到二者的区别
8、slice():提取字符串片段
并在新的字符串中返回被提取的部分
(1)只有一个参数时
从指定下标处开始截取后面的所有字符
const str = "11hello" console.log(str.slice(2));//hello
(2)两个参数时
第一个参数是起始位置,第二个参数是结束位置,取的是[n,m)
const str = "11hello" console.log(str.slice(2,5));//hel
9、split():把字符分割成数组
const str = "11hello" console.log(str.split(''));//["1", "1", "h", "e", "l", "l", "o"]
其中split()中的''是用来分割字符串的,意思就是用什么去分割字符串,前提是字符串中必须存在split()中写入的标识
const str = "h,e,l,l o" const str1 = str.split(",") console.log(str1);//["h", "e", "l", "l o"]
10、toUpperCase(),toLocaleUpperCase():将字符串转换成大写
const str = "hello" console.log(str.toUpperCase());//HELLO console.log(str.toLocaleUpperCase());//HELLO
两个都是大写,区别在于toLocaleUpperCase()在一些特殊的字符上,针对特定的地区支持的一种方法,一般来说,在不知道自己的代码将在那种语言环境中运行的情况下,还是使用针对地区的方法更稳妥一些
11、toLowerCase(),toLocaleLowerCase():将字符串转换成小写
const str = "HELLO" console.log(str.toLowerCase());//hello console.log(str.toLocaleLowerCase());//hello
情况与上述一样
12、substr():从起始索引号提取字符串中指定数目的字符
第一个参数是起始位置,第二个参数是长度,空格也算一个字符长度
var str = "hello world" var str3 = str.substr(3, 6); console.log(str3);//lo wor
13、substring():提取字符串中两个指定索引号之间的字符
[n,m)
var str = "hello world" var str4 = str.substring(3, 7); console.log(str4);//lo w
总结
1、不包含参数的方法
(1)toUpperCase(),toLocaleUpperCase()
(2)toLowerCase(),toLocaleLowerCase()
2、包含一个参数的方法
(1)charAt(指定位置(数字))
(2)charCodeAt(指定位置(数字))
(3)indexOf(字符)
(4)match(匹配值或正则表达式)
(5)search(字符)
(6)split(分隔的方式)
(7)slice(起始位置)
3、包含两个参数的方法
(1)replace(要替换的字符串,替换的字符串)
(2)slice(起始位置,结束位置) 取值[起始位置,结束位置)
(3)substr(起始位置,提取长度)
(4)substring(起始位置,结束位置) 取值[起始位置,结束位置)
4、包含多个参数
(1)concat(字符串1,字符串2,字符串3......)