静态方法
String.fromCharCode()
该方法的参数是一个或多个数值,代表 Unicode 码点,返回值是这些码点组成的字符串。
String.fromCharCode() // ""
String.fromCharCode(97) // "a"
String.fromCharCode(104, 101, 108, 108, 111) // "hello"
实例属性
String.prototype.length
'abc'.length // 3
实例方法
- 字符
charAt
charCodeAt
'abc'.charAt(1) // "b" 返回对应位置的字符 等同于 'acb'[1]
'abc'.charCodeAt(1) // 98 根据位置返回编号
- concat
连接两个字符串,返回一个新字符串,不改变原字符串。
var s1 = 'abc';
var s2 = 'def';
s1.concat(s2) // "abcdef"
s1 // "abc"
- 截取
slice
substring
substr
- 位置
indexof
lastIndexOf
-
trim
-
匹配
match
match方法用于确定原字符串是否匹配某个子字符串,返回一个数组,成员为匹配的第一个字符串。如果没有找到匹配,则返回null。
'cat, bat, sat, fat'.match('at') // ["at"]
'cat, bat, sat, fat'.match('xt') // null
返回的数组还有index属性和input属性,分别表示匹配字符串开始的位置和原始字符串。
var matches = 'cat, bat, sat, fat'.match('at');
matches.index // 1
matches.input // "cat, bat, sat, fat"
match正则g用法
有g,表示获取有满足整个正则表达式的字符串的数组
let str = "UM_distinctid=164f9bc2f88952-0f501a98bc757e-51422e1f-1fa400-164f9bc2f8933f; Sa_BrandId_65=193313; Sa_IsAdmin_65=true; dms_token=ed13ad66cd474026a9ebc5312734d4ba; SdCloud_BrandId=193313; SdCloud_Token=5dd9701bad2148df8d02cf96ba6e8501; Sa_Token_65=3a3c87a5922446f6ae0926af8323a330; Sa_Token_35=4662266b1f60418ca0ee98c15cacdb2b; Sa_BrandId_35=193313; Sa_IsAdmin_35=true; Sa_Token=657e78058e724f8487c3ebd334981c0b; Sa_BrandId=193313; Sa_IsAdmin=true";
let ary1 = str.match(/Sa_Token(\_\d{2})?/g); // ["Sa_Token_65", "Sa_Token_35", "Sa_Token"]
let ary2 = str.match(/Sa_Token(\_\d{2})?/); // ["Sa_Token_65", "_65", index: 234, input: "UM_distinctid=164f9bc2f88952-0f501a98bc757e-51422e…3ebd334981c0b; Sa_BrandId=193313; Sa_IsAdmin=true", groups: undefined]
replace
replace方法用于替换匹配的子字符串,一般情况下只替换第一个匹配(除非使用带有g修饰符的正则表达式)。
'aa'.replace('a','b') // 'ba'
'aa'.replace(/a/g,'b') // 'bb'
search
search方法的用法基本等同于match,但是返回值为匹配的第一个位置。如果没有找到匹配,则返回-1。
-
split
‘abc’.split(’’) // [‘a’, ‘b’, ‘c’]
‘a,b,c’.split(’,’) // [‘a’, ‘b’, ‘c’]
localeCompare 比较
大小写(toLowerCase 和 toUpperCase)