javascript基础系列之String

javascript基础系列之String

描述

定义:保存以文本形式表示的数据

常见操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 // 从字符串中获取单个字符
console.log('apple'.charAt(3)); // 'l'
console.log('apple'[3]); // 'l' 字符串可以当成一个类数组

// 字符串的比较 >, <, >=, <=, 字符串实例localeCompare方法能获取同样的结果
console.log("a" > "b"); // false

// 基本字符串和字符串对象的区别
var str = "1 + 2";
var str1 = new String("1 + 2");
console.log(typeof str); // string
console.log(typeof str1); // object 字符串对象实例

// eval执行结果不一样
console.log(eval(str)); // 3
console.log(eval(str1)); // String {'1+2'}
console.log(eval(str1.valueOf())); // 3 valueOf方法可以把字符串对象转换为对应的基本字符串

构造函数String

作为函数调用进行类型转换

静态方法

String.fromCharCode

1
2
// String.fromCharCode 通过一串Unicode创建字符串
console.log(String.fromCharCode(189, 43, 190, 61)); // ½+¾=

String.fromCodePoint

1
2
// String.fromCodePoint 通过一串码点创建字符串
console.log(String.fromCodePoint(9731, 9733, 9842, 0x2F804)); // ☃★♲你

String.raw

1
2
// String.raw 通过模板字符串创建字符串
console.log(String.raw`Hi\n${2+3}!`); // Hi\n5!

实例属性

String.prototype.length

1
2
3
// String.prototype.length 获取字符串长度,只读属性
let str = "abc";
console.log(str.length); // 3

实例方法

String.prototype.[at|charAt|charCodeAt|codePointAt]

1
2
3
4
5
6
// String.prototype.[at|charAt|charCodeAt|codePointAt]
let str = "hello world";
console.log(str.at(6)); // w
console.log(str.charAt(6)); // w
console.log(str.charCodeAt(6)); // 119, UTF-16编码单元, 表示字符串‘w’
console.log(str.codePointAt(6)); // 119, UTF-16编码单元,表示字符串'w'

String.prototype.concat

1
2
3
4
// String.prototype.concat 连接多个字符串,返回一个新的字符串
let str1 = "hello";
let str2 = "world";
console.log(str1.concat(",", str2)); // hello,world

String.prototype.includes

1
2
3
// String.prototype.includes  判断一个字符串是否在另一个字符串中,返回布尔值
let str = "hello";
console.log(str.includes("el", 2)); // false, 第二个参数表示查询起始位置

String.prototype.[startsWith|endsWith]

1
2
3
4
5
6
7
8
// String.prototype.startsWith  判断当前字符串是否以指定子字符串开头
const str3 = "hello world";
console.log(str3.startsWith("hel")); // true

// String.prototype.endsWith 判断字符串结尾字符串,返回布尔值
let str = "hello world";
console.log(str.endsWith("ld")); // true

String.prototype.indexOf

1
2
3
4
5
// String.prototype.indexOf  在字符串对象中第一次出现指定值的索引,未找到返回-1
// String.prototype.lastIndexOf 在字符串对象中最后一次出现指定值的索引,未找到返回-1
let str = "hello world";
console.log(str.indexOf("l", 4)); // 9, 第二个参数表示查询开始位置
console.log(str.lastIndexOf("l", 8)); // 3, 第二个参数表示查询开始位置,从后往前查找,从0开始

String.prototype.localeCompare

1
2
3
4
// String.prototype.localeCompare 两个字符串比较,返回-1,0,1
console.log('a'.localeCompare('c')); // -1, 在参数'c'之前
console.log('check'.localeCompare('against')); // 1,在参数‘against’之前
console.log('a'.localeCompare('a')); // 0,两个元素相等

String.prototype.match

1
2
3
// String.prototype.match 字符串匹配正则表达式
const str = "Hello World";
console.log(str.match(/[A-Z]/g)); // ["H", "W"]

String.prototype.matchAll

1
2
3
// String.prototype.matchAll 返回所有匹配正则表达式的结果及分组捕获组的迭代器
const str = "Hello World";
console.log([...str.matchAll(/[A-Z]/g)]); // <img src="./130.png" />

String.prototype.normalize

1
2
3
4
5
6
7
8
// String.prototype.normalize 按照指定Unicode形式("NFC"、"NFD"、"NFKC",或 "NFKD")将当前字符串正规化
const name1 = '\u0041\u006d\u00e9\u006c\u0069\u0065';
const name2 = '\u0041\u006d\u0065\u0301\u006c\u0069\u0065';
const name1NFC = name1.normalize('NFC');
const name2NFC = name2.normalize('NFC');
console.log(`${name1NFC},${name2NFC}`); // Amélie,Amélie
console.log(name1 === name2); // false
console.log(name1NFC === name2NFC); // true

String.prototype[padStart}|padEnd]

1
2
3
4
5
// String.prototype.padEnd(targetLength [, padString]) 指定一个长度,用一个字符串(第二个参数)从右边开始填充当前字符串
// String.prototype.padStart(targetLength [, padString]) 指定一个长度,用一个字符串(第二个参数)从左边开始填充当前字符串
const str = "hello world";
console.log(str.padEnd(15, ".")); // hello world....
console.log(str.padStart(15, ".")); // ....hello world

String.prototype.repeat

1
2
3
// String.prototype.repeat(count) 重复字符串count次
const str = "hello~";
console.log(str.repeat(2)); // hello~hello~

String.prototype.[replace|replaceAll]

1
2
3
4
5
// String.prototype.[replace|replaceAll]  根据参数替换字符串,第一个参数可以是字符串或者正则表达式,replace和replaceAll正则表达式没区别,因为正则表达式可以/a/g全局匹配
const str = "hello 123";
console.log(str.replace("l", "c")); // heclo 123 只替换第一个匹配项
console.log(str.replaceAll("l", "c")); // hecco 123 匹配多个。和replace的区别是匹配字符串
console.log(str.replace(/[0-9]/g, "a")); // hello aaa
1
2
3
// String.prototype.search 参数是正则表达式,如果非正则会隐式转换, 返回正则表达式第一次匹配的位置,匹配不到返回-1
const str = "hello2 worl3d";
console.log(str.search(/[0-9]/)); // 5

String.prototype.slice

1
2
3
// String.prototype.slice(beginIndex,endIndex) 提取字符串的一部分,返回一个新的字符串,不会改变原字符串。
const str1 = "hello world";
console.log(str1.slice(2, 4)); // ll

String.prototype.split

1
2
3
// String.prototype.split   根据指定字符串把原字符串分割成数组
const str2 = "hello world !";
console.log(str2.split(" ")); // ["hello", "world", "!"]

String.prototype.substring

1
2
3
// String.prototype.substring(indexStart, indexEnd) 字符串截取,从开始索引indexStart到结束索引(indexEnd)的位置
const str = "hello world";
console.log(str.substring(2, 4)); // ll

String.prototype[toLocaleLowerCase|toLocaleLowerCase]

1
2
3
4
5
// String.prototype.toLocaleLowerCase  // 字符串转成小写,针对特定地区的实现
// String.prototype.toLocaleUpperCase // 字符串转成大写,针对特定地区的实现
const str1 = "Hello World";
console.log(str1.toLocaleLowerCase()); // hello world
console.log(str1.toLocaleUpperCase()); // HELLO WORLD

String.prototype[toLowerCase|toUpperCase]

1
2
3
4
5
// String.prototype.toLowerCase  // 字符串转成小写
// String.prototype.toUpperCase // 字符串转成大写
const str2 = "Hello World";
console.log(str2.toLowerCase()); // hello world
console.log(str2.toUpperCase()); // HELLO WORLD

String.prototype.toString

1
2
3
 // String.prototype.toString  把变量转换成字符串,重写了Object.prototype.toString方法
const str3 = new String("hello world"); // 字符串变量
console.log(str3.toString());

String.prototype[trim|trimStart|trimEnd]

1
2
3
4
5
6
7
8
9
10
11
// String.prototype.trim  删除字符串两端的空白字符串
// String.prototype.trimStart 删除字符串开头的空格
// String.prototype.trimEnd 删除字符串结尾的空格
const str = " hello world ";
function log(str) {
console.log("@" + str + "#")
}
log(str.trim()); //@hello world#
log(str); //@ hello world # 不影响原字符串
log(str.trimStart()); // @hello world #
log(str.trimEnd()); // @ hello world#

String.prototype.valueOf

1
2
3
// String.prototype.valueOf  返回String对象的原始值
const str = new String("hello");
console.log(str.valueOf()); // hello

String.prototype[@@iterator]

1
2
3
4
5
6
// String.prototype[@@iterator] 在String实例上部署了遍历器
const str1 = "hello world";
const iterator = str1[Symbol.iterator]();
console.log(iterator.next()) // {done: false, value: 'h'}
console.log(iterator.next()) // {done: false, value: 'e'}

参考文档

String