JS 字符串方法
String类定义的方法都不能改变字符串的内容
方法 | 描述 |
---|---|
字符 |
|
charAt(3) | 返回在指定位置的字符 =>'g' |
charCodeAt(3) | 返回指定字符的 Unicode 编码 =>103 |
fromCharCode(103) | 将字符编码转换为一个字符串 =>'g' |
查找位置 |
|
indexOf() | 下标/-1 |
lastIndexOf() | 下标/-1 |
正则 |
|
search(正则) | 下标/-1 |
match(正则) | 返回数组 |
剪切 |
|
slice(首 , 尾) | 可负值 |
subString(首 , 尾) | 不可负值 |
subStr(首 , 长) | |
trim() | 去除首尾空白 |
拼接 |
|
concat('ab' , 'cd') | 拼接 |
repeat(2) | 重复2次 |
替换,大小写敏感 |
|
replace(新 , 旧) str.replace("Microsoft", "W3School") |
只替换第一个匹配 |
replaceAll(新 , 旧) | 替换全部 |
转换成数组 |
|
split('') | 以字符''分割, 如split('&'),返回数组 |
其他 |
|
toLowCase()/toUpperCase() | 转换大小写 |
includes() | ture/false |
startsWith() | ture/false |
endsWith() | ture/false |