一、charAt(索引值)
1.定义:返回指定位置的字符;简单说就是,已知索引,求索引对应的字符;
2.用法:var str='abcdefg';console.log(str.charAt(2))//c 返回对应的字符;
二、charCodeAt(索引值)
1.charCodeAt()定义:获取指定位置字符的Unicode编码:
2.Unicode编码定义:又叫万国码、统一码、单一码;它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。
3.charCodeAt()用法:var str2="张三”; console.log(str2.charCodeAt(1)) //19977返回对应的Unicode编码
三、substr(起始索引值,要截取的长度);
1.定义:从起始位置截取指定长度的字符;
2.用法:var str='abcdefg'; console.log(str.substr(2,4))//'cdef' ;如果只写一个值,默认从写的索引值截取到最后
四、substring(起始索引,结束索引)
1.定义:截取开始位置到结束位置的一段字符;包前不包后,算是起始位置对应的字符,不算结束位置的字符
2.用法:var str='abcdefg'; console.log(str.substr(2,4))//'cd' ;如果只写一个值,默认从写的索引值截取到最后
五、toUpperCase()
1.定义:将字符串转大写
2.用法:var str='abc';console.log(str.toUpperCase());// 'ABC'
六、toLowerCase()
1.定义:字符串转小写
2.用法:var str='ABC';console.log(str.toUpperCase());// 'abc'
七、indexOf('内容')
1.定义:(1)如果找到,返回对应内容的索引值;(2)如果没找到,返回 -1
2.用法:var str='hello world';
console.log(str.indeOf('w')) // 6
console.log(str.indexOf('z')) // -1
console.log(str.indexOf(' ')) // 5 ;可以查找空格
八、search()
1.定义:检索指定字符串对应的位置
2.用法:var str='hello world';
console.log(str.search('w')) // 6
console.log(str.search('z')) // -1
console.log(str.search(' ')) // 5 ;可以查找空格
发现与indexOf()的用法很像,但是区别在于如果只是查找简单的字符串,使用indexOf对系统的销消耗更小,search主要是检索正则的
九、replace
1.定义:替换某些字符串
2.用法:var str4='hello world';
console.log(str4.replace('world',''))//hello
console.log(str4.replace('world','张三'))//hello 张三
十、match()
1.定义:主要用于字符串和正则的配合使用;查询字符串中是否有匹配正则的值
2.用法:var str4='hello world';
console.log(str4.match('w'))//[0: "w",groups: undefined,index: 6,input: "hello world",length: 1]
console.log(str4.match('world'))//[0: "world",groups: undefined,index: 6,input: "hello world",length: 1]
console.log(str4.match('z'))// null
十一、slice(起始位置,结束位置)
1.定义:截取字符串
2.用法 var str4='hello world';
console.log(str4.slice(0,5)) //hello
console.log(str4) //hello world
十二、split()
1.定义:拆分字符串成为数组
2.用法:var str4='hello world';
console.log(str4.split('ll'))//['he', 'o world']
console.log(str4.split(''))//['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']