String

每个string实例都有length属性

  1. let stringValue = "hello world";
  2. console.log(stringValue.length); // "11"

每个字符的基本长度是16位

charAt()

  1. let message = "abcde";
  2. console.log(message.charAt(2)); // "c"

charCodeAt()

utf-16

  1. let message = "abcde";
  2. // Unicode "Latin small letter C" is U+0063
  3. console.log(message.charCodeAt(2)); // 99

fromCharCode()

由UTF-16编码转成字符串

  1. console.log(String.fromCharCode(0x61, 0x62, 0x63, 0x64, 0x65)); // "abcde"

Unicode增补码用32位表示,比如表情,length算 2

  1. let message = "ab☺de";
  2. console.log(message.length); // 6

charAt()无法解析32位增补码,用codePointAt()
fromCharCode()可以正确解析,同时能用fromCodePoint()
**

concat()拼接多个字符串

不会改变原字符串

  1. let stringValue = "hello ";
  2. let result = stringValue.concat("world", "!");
  3. console.log(result); // "hello world!"
  4. console.log(stringValue); // "hello"

slice(),subString(),subStr()

slice和subString的第二个参数都是index,
subStr的第二个参数是长度
三个方法都不会改变原字符串

indexOf(), lastIndexOf()

返回字符所在位置, 不存在返回-1
第二个参数可以指定起始位置

  1. let a = "hello world or hello kitty"
  2. a.indexOf('or') // 7
  3. a.lastIndexOf('or') // 12
  4. a.indexOf('or', 6) // 7
  5. a.lastIndexOf('or', 9) // 7

startsWith(), endsWith, includes()

starts从起始位置查找,endsWith从末尾查找,includes查找整个字符串,存在返回true,否则返回false
startsWith和includes第二个参数可以指定起始位置
endsWith第二个参数可以指定结束位置

  1. let a = "foobarbaz"
  2. a.startsWith('foo') // true
  3. a.startsWith('foo', 2) // false
  4. a.endsWith('baz') // true
  5. a.endsWith('baz', 8) // false
  6. a.includes('bar') // true

trim()

trim(), trimLeft(), trimRight() 去除两边的空格,返回一个新字符串,不会改变原字符串

  1. let stringValue = " hello world ";
  2. let trimmedStringValue = stringValue.trim();
  3. console.log(stringValue); // " hello world "
  4. console.log(trimmedStringValue); // "hello world"

repeat()

  1. let stringValue = "na ";
  2. console.log(stringValue.repeat(16) + "batman");
  3. // na na na na na na na na na na na na na na na na batman

padStart(), padEnd()

多则填充,少则截取

  1. let stringValue = "foo";
  2. console.log(stringValue.padStart(8, "bar")); // "barbafoo"
  3. console.log(stringValue.padStart(2)); // "foo"
  4. console.log(stringValue.padEnd(8, "bar")); // "foobarba"
  5. console.log(stringValue.padEnd(2)); // "foo"

Iterators(迭代) and Destructuring(解构)

在for循环里使用迭代器

  1. for (const c of "abcde") {
  2. console.log(c);
  3. }

解构赋值

  1. let message = "abcde";
  2. console.log([...message]); // ["a", "b", "c", "d", "e"]
  3. var a, b, rest;
  4. [a, b] = [10, 20];
  5. console.log(a); // 10
  6. console.log(b); // 20
  7. [a, b, ...rest] = [10, 20, 30, 40, 50];
  8. console.log(a); // 10
  9. console.log(b); // 20
  10. console.log(rest); // [30, 40, 50]
  11. ({ a, b } = { a: 10, b: 20 }); // 括号必须有
  12. console.log(a); // 10
  13. console.log(b); // 20
  14. ({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
  15. console.log(a); // 10
  16. console.log(b); // 20
  17. console.log(rest); // {c: 30, d: 40}

大小写转换

toLocaleUpperCase()和toLocaleLowerCase()会根据不同语言做对应转换

  1. let stringValue = "hello world";
  2. console.log(stringValue.toLocaleUpperCase()); // "HELLO WORLD"
  3. console.log(stringValue.toUpperCase()); // "HELLO WORLD"
  4. console.log(stringValue.toLocaleLowerCase()); // "hello world"
  5. console.log(stringValue.toLowerCase()); // "hello world"

match(), search(), replace()

match(), search()接受正则表达式作为参数,返回Index

  1. let text = "cat, bat, sat, fat";
  2. let pos = text.search(/at/);
  3. console.log(pos); // 1

replace默认只替换第一个,正则加g则替换所有

  1. let text = "cat, bat, sat, fat";
  2. let result = text.replace("at", "ond");
  3. console.log(result); // "cond, bat, sat, fat"
  4. result = text.replace(/at/g, "ond");
  5. console.log(result); // "cond, bond, sond, fond"

localeCompare()

和参数比较,如果排在参数前,则返回负数,相同返回0,在后返回正数

  1. let stringValue = "yellow";
  2. console.log(stringValue.localeCompare("brick")); // 1
  3. console.log(stringValue.localeCompare("yellow")); // 0
  4. console.log(stringValue.localeCompare("zoo")); // -1