属性
constructor
:构造函数 。__proto__
:指向构造函数的原型对象 。length
:字符串长度 。
方法
查找
carharAt(i)
:返回指定位置对应的字符 。let a = [1,2,8,4,5,6,0,8] let b = a.toString() let c = b.charAt(2) console.log( c )
charCodeAt(i)
:返回指定位置字符对应的ASCLL码值 。let a = [1,2,8,4,5,6,0,8] let b = a.toString() let c = b.charCodeAt(2) console.log( c )
string.fromCharCode(50)
:将ASCLL码值转化为字符 。let a = String.fromCharCode(50) console.log(a);
indexOf(str1)
:返回指定字符str1
在字符串中首次出现的位置let a = '1,2,8,4,5,6,2,8' let b = a.indexOf('2') console.log(b);
lastIndexOf(str1)
:返回指定字符在字符串中最后一次出现的位置,否则返回 -1 。let a = '1,2,8,4,5,6,2,8' let b = a.lastIndexOf('8') console.log(b);
includes(str1)
:判断字符串中是否包含str1,若包含返回true,否则返回false 。let a = '1,2,8,4,5,6,2,8' let b = a.includes('9') console.log(b);
替换
replace(str1,str2)
:用str2替换str1在字符串中首次出现的位置,不影响原字符串 。let a = '1,2,8,4,5,6,2,8' let b = a.replace(2,9) console.log(b); console.log(a);
重复
repeat(num)
:字符串重复出现num次 。let a = '1,2,8,4,5,6,2,8' let b = a.repeat(2) console.log(b); console.log(a);
转换
str.toUpperCase()
:转换为大写 。let a = 'a,b,c,d,e,f,8' let b = a.toUpperCase() console.log(b)
sre.toLowerCase()
:转换为小写 。let a = 'E,D,C,G,U,I,8' let b = a.toLowerCase() console.log(b)
split()
:按照指定的标识符,将字符串转换为数组 。let a = 'E,D,C,G,U,I,8' let b = a.split() console.log(b)
截取
slice(start,end)
:截取从start
到end
,但是不包括end
位置字符,不影响原字符串 。let a = 'E,D,C,G,U,I,8' let b = a.slice(1,5) console.log(b) console.log(a)
substring(start,end)
:不支持负数let a = 'E,D,C,G,U,I,8' let b = a.substring(1,5) console.log(b) console.log(a)
substr(start,length)
:从start
开始截取指定长度 。let a = 'E,D,C,G,U,I,8' let b = a.substr(1,5) console.log(b) console.log(a)