通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
[TOC] ## 静态方法 ### String.fromCharCode() unicode转字符 ``` String.fromCharCode(104, 101, 108, 108, 111) //"hello" ``` ## 实例方法 ### String.prototype.charAt() 字符所在位置 ``` var s = new String('abc'); //= s[1] //"b" s.charAt(1) // "b" s.charAt(s.length - 1) // "c" ``` ### String.prototype.charCodeAt() 返回unicode 码 ``` 'abc'.charCodeAt(1) // 98 ``` ### String.prototype.concat() 连接字符串 ``` 'a'.concat('b', 'c') // "abc" ``` ### String.prototype.slice()/substring()/substr() 两个参数,第一个是开始位,第二个是结束位置,不改变原字符串 ``` 'JavaScript'.slice(0, 4) // "Java" ``` ### String.prototype.indexOf()/lastIndexOf() ``` 'hello world'.indexOf('o') // 4 'JavaScript'.indexOf('script') // -1 'hello world'.lastIndexOf('o') // 7 ``` `indexOf`方法还可以接受第二个参数 ``` 'hello world'.indexOf('o', 6) // 7 ``` lastIndexOf的第二个参数表示从该位置起向前匹配 ``` 'hello world'.lastIndexOf('o', 6) // 4 ``` ### String.prototype.trim() 去除空格 trim方法用于去除字符串两端的空格 ``` ' hello world '.trim() //hello word ``` ``` '\r\nabc \t'.trim() // 'abc' ``` ### String.prototype.toLowerCase()/String.prototype.toUpperCase() ``` 'cat, bat, sat, fat'.match('at') // ["at"] 'cat, bat, sat, fat'.match('xt') // null ``` 可匹配正则 ``` var s ="hello wordd"; s.match(/w.*d/);//[ 'word', index: 6, input: 'hello wordd', groups: undefined ] ```