String
每个string实例都有length属性
let stringValue = "hello world";
console.log(stringValue.length); // "11"
charAt()
let message = "abcde";
console.log(message.charAt(2)); // "c"
charCodeAt()
utf-16
let message = "abcde";
// Unicode "Latin small letter C" is U+0063
console.log(message.charCodeAt(2)); // 99
fromCharCode()
由UTF-16编码转成字符串
console.log(String.fromCharCode(0x61, 0x62, 0x63, 0x64, 0x65)); // "abcde"
Unicode增补码用32位表示,比如表情,length算 2
let message = "ab☺de";
console.log(message.length); // 6
charAt()无法解析32位增补码,用codePointAt()
fromCharCode()可以正确解析,同时能用fromCodePoint()
**
concat()拼接多个字符串
不会改变原字符串
let stringValue = "hello ";
let result = stringValue.concat("world", "!");
console.log(result); // "hello world!"
console.log(stringValue); // "hello"
slice(),subString(),subStr()
slice和subString的第二个参数都是index,
subStr的第二个参数是长度
三个方法都不会改变原字符串
indexOf(), lastIndexOf()
返回字符所在位置, 不存在返回-1
第二个参数可以指定起始位置
let a = "hello world or hello kitty"
a.indexOf('or') // 7
a.lastIndexOf('or') // 12
a.indexOf('or', 6) // 7
a.lastIndexOf('or', 9) // 7
startsWith(), endsWith, includes()
starts从起始位置查找,endsWith从末尾查找,includes查找整个字符串,存在返回true,否则返回false
startsWith和includes第二个参数可以指定起始位置
endsWith第二个参数可以指定结束位置
let a = "foobarbaz"
a.startsWith('foo') // true
a.startsWith('foo', 2) // false
a.endsWith('baz') // true
a.endsWith('baz', 8) // false
a.includes('bar') // true
trim()
trim(), trimLeft(), trimRight() 去除两边的空格,返回一个新字符串,不会改变原字符串
let stringValue = " hello world ";
let trimmedStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimmedStringValue); // "hello world"
repeat()
let stringValue = "na ";
console.log(stringValue.repeat(16) + "batman");
// na na na na na na na na na na na na na na na na batman
padStart(), padEnd()
多则填充,少则截取
let stringValue = "foo";
console.log(stringValue.padStart(8, "bar")); // "barbafoo"
console.log(stringValue.padStart(2)); // "foo"
console.log(stringValue.padEnd(8, "bar")); // "foobarba"
console.log(stringValue.padEnd(2)); // "foo"
Iterators(迭代) and Destructuring(解构)
在for循环里使用迭代器
for (const c of "abcde") {
console.log(c);
}
解构赋值
let message = "abcde";
console.log([...message]); // ["a", "b", "c", "d", "e"]
var a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]
({ a, b } = { a: 10, b: 20 }); // 括号必须有
console.log(a); // 10
console.log(b); // 20
({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
大小写转换
toLocaleUpperCase()和toLocaleLowerCase()会根据不同语言做对应转换
let stringValue = "hello world";
console.log(stringValue.toLocaleUpperCase()); // "HELLO WORLD"
console.log(stringValue.toUpperCase()); // "HELLO WORLD"
console.log(stringValue.toLocaleLowerCase()); // "hello world"
console.log(stringValue.toLowerCase()); // "hello world"
match(), search(), replace()
match(), search()接受正则表达式作为参数,返回Index
let text = "cat, bat, sat, fat";
let pos = text.search(/at/);
console.log(pos); // 1
replace默认只替换第一个,正则加g则替换所有
let text = "cat, bat, sat, fat";
let result = text.replace("at", "ond");
console.log(result); // "cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
console.log(result); // "cond, bond, sond, fond"
localeCompare()
和参数比较,如果排在参数前,则返回负数,相同返回0,在后返回正数
let stringValue = "yellow";
console.log(stringValue.localeCompare("brick")); // 1
console.log(stringValue.localeCompare("yellow")); // 0
console.log(stringValue.localeCompare("zoo")); // -1