javaScript:(Array,Boolean,Date,Math,Number,String )对象方法集合(详细版)

目录

Array 对象

1. 修改数组的方法

2. 访问数组的方法

3. 遍历数组的方法

4. 查找数组的方法

5. 其他方法

6. 静态方法

7. 数组的扩展运算符(...)

Boolean对象

Date 对象

1. 获取日期和时间的方法

2. 设置日期和时间的方法

3. 日期格式化方法

4. 静态方法

Math 对象

1. 数学常量

2. 数学方法

2.1 取整方法

2.2 最大值和最小值

2.3 随机数

2.4 幂和开方

2.5 对数和指数

2.6 三角函数

2.7 双曲函数

2.8 其他方法

Number 对象

1. Number 对象的实例方法

2. Number 的静态属性

3. Number 的静态方法

String 对象

1. String 对象的实例方法

1.1 获取字符串信息

1.2 查找和匹配

1.3 截取和分割

1.4 替换和修改

1.5 其他方法

2. String 的静态方法


Array 对象

1. 修改数组的方法

  • push(): 向数组末尾添加一个或多个元素,并返回新的长度。

    let arr = [1, 2, 3];
    arr.push(4); // arr 现在是 [1, 2, 3, 4]
  • pop(): 移除并返回数组的最后一个元素。

    let arr = [1, 2, 3];
    let last = arr.pop(); // last 是 3, arr 现在是 [1, 2]
  • shift(): 移除并返回数组的第一个元素。

    let arr = [1, 2, 3];
    let first = arr.shift(); // first 是 1, arr 现在是 [2, 3]
  • unshift(): 向数组开头添加一个或多个元素,并返回新的长度。

    let arr = [1, 2, 3];
    arr.unshift(0); // arr 现在是 [0, 1, 2, 3]
  • splice(): 通过删除或替换现有元素或添加新元素来修改数组。

    let arr = [1, 2, 3, 4];
    arr.splice(1, 2, 'a', 'b'); // arr 现在是 [1, 'a', 'b', 4]
  • reverse(): 反转数组中的元素顺序。

    let arr = [1, 2, 3];
    arr.reverse(); // arr 现在是 [3, 2, 1]
  • sort(): 对数组元素进行排序。

    let arr = [3, 1, 4, 2];
    arr.sort(); // arr 现在是 [1, 2, 3, 4]

2. 访问数组的方法

  • concat(): 合并两个或多个数组,并返回新数组。

    let arr1 = [1, 2];
    let arr2 = [3, 4];
    let newArr = arr1.concat(arr2); // newArr 是 [1, 2, 3, 4]
  • slice(): 返回数组的一部分的浅拷贝。

    let arr = [1, 2, 3, 4];
    let subArr = arr.slice(1, 3); // subArr 是 [2, 3]
  • indexOf(): 返回数组中指定元素的第一个索引,如果不存在则返回 -1。

    let arr = [1, 2, 3, 2];
    let index = arr.indexOf(2); // index 是 1
  • lastIndexOf(): 返回数组中指定元素的最后一个索引,如果不存在则返回 -1。

    let arr = [1, 2, 3, 2];
    let lastIndex = arr.lastIndexOf(2); // lastIndex 是 3
  • includes(): 判断数组是否包含某个元素,返回布尔值。

    let arr = [1, 2, 3];
    let hasTwo = arr.includes(2); // hasTwo 是 true

3. 遍历数组的方法

  • forEach(): 对数组中的每个元素执行一次提供的函数。

    let arr = [1, 2, 3];
    arr.forEach(item => console.log(item)); // 依次输出 1, 2, 3
  • map(): 创建一个新数组,其结果是该数组中的每个元素调用一次提供的函数后的返回值。

    let arr = [1, 2, 3];
    let newArr = arr.map(item => item * 2); // newArr 是 [2, 4, 6]
  • filter(): 创建一个新数组,包含通过所提供函数测试的所有元素。

    let arr = [1, 2, 3, 4];
    let filteredArr = arr.filter(item => item > 2); // filteredArr 是 [3, 4]
  • reduce(): 对数组中的每个元素执行一个 reducer 函数,将其结果汇总为单个返回值。

    let arr = [1, 2, 3, 4];
    let sum = arr.reduce((acc, item) => acc + item, 0); // sum 是 10
  • reduceRight(): 与 reduce() 类似,但从右到左执行。

    let arr = [1, 2, 3, 4];
    let sum = arr.reduceRight((acc, item) => acc + item, 0); // sum 是 10
  • every(): 测试数组中的所有元素是否都通过了指定函数的测试。

    let arr = [1, 2, 3, 4];
    let allGreaterThanZero = arr.every(item => item > 0); // allGreaterThanZero 是 true
  • some(): 测试数组中是否至少有一个元素通过了指定函数的测试。

    let arr = [1, 2, 3, 4];
    let someGreaterThanThree = arr.some(item => item > 3); // someGreaterThanThree 是 true
  • at():通过索引访问数组中的元素,支持负索引(从数组末尾开始计数)。

     let arr = [1, 2, 3, 4];
     console.log(arr.at(1));  // 2
     console.log(arr.at(-1)); // 4

4. 查找数组的方法

  • find(): 返回数组中满足提供的测试函数的第一个元素的值。

    let arr = [1, 2, 3, 4];
    let found = arr.find(item => item > 2); // found 是 3
  • findIndex(): 返回数组中满足提供的测试函数的第一个元素的索引。

    let arr = [1, 2, 3, 4];
    let index = arr.findIndex(item => item > 2); // index 是 2

5. 其他方法

  • join(): 将数组中的所有元素连接成一个字符串。

    let arr = [1, 2, 3];
    let str = arr.join('-'); // str 是 "1-2-3"
  • length:获取数组的长度。

     let arr = [1, 2, 3];
     console.log(arr.length); // 3
  • toString(): 返回数组的字符串表示。

    let arr = [1, 2, 3];
    let str = arr.toString(); // str 是 "1,2,3"
  • toLocaleString(): 返回数组的本地化字符串表示。

    let arr = [1, 2, 3];
    let str = arr.toLocaleString(); // str 是 "1,2,3"(根据本地化设置)
  • flat(): 将嵌套数组扁平化,返回一个新数组。

    let arr = [1, [2, [3]]];
    let flatArr = arr.flat(2); // flatArr 是 [1, 2, 3]
  • flatMap(): 首先使用映射函数映射每个元素,然后将结果扁平化一层。

    let arr = [1, 2, 3];
    let flatMappedArr = arr.flatMap(item => [item * 2]); // flatMappedArr 是 [2, 4, 6]
  • fill(): 用一个固定值填充数组中的元素。

    let arr = [1, 2, 3];
    arr.fill(0); // arr 现在是 [0, 0, 0]
  • copyWithin(): 浅复制数组的一部分到同一数组中的另一个位置,并返回它,而不修改其大小。

    let arr = [1, 2, 3, 4, 5];
    arr.copyWithin(0, 3); // arr 现在是 [4, 5, 3, 4, 5]
  • toReversed():返回一个反转后的新数组,原数组不变。

     let arr = [1, 2, 3];
     let reversed = arr.toReversed(); // [3, 2, 1]
     console.log(arr); // [1, 2, 3](原数组不变)
  • toSorted():返回一个排序后的新数组,原数组不变。

     let arr = [3, 1, 2];
     let sorted = arr.toSorted(); // [1, 2, 3]
     console.log(arr); // [3, 1, 2](原数组不变)
  • toSpliced():返回一个修改后的新数组(添加、删除或替换元素),原数组不变。

     let arr = [1, 2, 3, 4];
     let spliced = arr.toSpliced(2, 1, "a"); // [1, 2, "a", 4]
     console.log(arr); // [1, 2, 3, 4](原数组不变) 
  • Array.from():从类数组对象或可迭代对象创建一个新的数组。

     let arr = Array.from("hello"); // ['h', 'e', 'l', 'l', 'o']
  • Array.of():创建一个具有可变数量参数的新数组,而不考虑参数的数量或类型。

     let arr = Array.of(1, 2, 3); // [1, 2, 3]
  • entries():返回一个新的数组迭代器对象,该对象包含数组中每个索引的键/值对。

     let arr = [1, 2, 3];
     for (let [index, value] of arr.entries()) {
       console.log(index, value);
     }
  • keys():返回一个新的数组迭代器对象,该对象包含数组中每个索引的键。

     let arr = [1, 2, 3];
     for (let key of arr.keys()) {
       console.log(key);
     }
  • values():返回一个新的数组迭代器对象,该对象包含数组中每个索引的值。

     let arr = [1, 2, 3];
     for (let value of arr.values()) {
       console.log(value);
     }

6. 静态方法

Array.from(arrayLike[, mapFn[, thisArg]])
  • arrayLike:类数组对象或可迭代对象(如字符串、Set、Map、NodeList 等)。

  • mapFn(可选):映射函数,可以对每个元素进行处理。

  • thisArg(可选):执行 mapFn 时的 this 值。

将类数组对象或可迭代对象转换为数组。

const str = 'hello';
const arr = Array.from(str);
console.log(arr); // 输出: ['h', 'e', 'l', 'l', 'o']

Array.from 的第二个参数是一个映射函数,可以对每个元素进行处理。

将字符串转换为大写

const str = 'hello';
const arr = Array.from(str, char => char.toUpperCase());
console.log(arr); // 输出: ['H', 'E', 'L', 'L', 'O']

将数字范围转换为数组

const length = 5;
const arr = Array.from({ length }, (_, index) => index + 1);
console.log(arr); // 输出: [1, 2, 3, 4, 5]

Array.from 可以通过传入一个带有 length 属性的对象来创建指定长度的数组。

创建长度为 number的数组,每个元素为 number

const length = number;
const arr = Array.from({ number }, () => number);
console.log(arr); // 输出:一个长度number并且每一项值都为number的数组
  • Array.isArray(): 判断一个值是否为数组。

    let arr = [1, 2, 3];
    let isArr = Array.isArray(arr); // isArr 是 true
  • Array.of(): 创建一个具有可变数量参数的新数组实例。

    let arr = Array.of(1, 2, 3); // arr 是 [1, 2, 3]

7. 数组的扩展运算符(...

扩展运算符可以用于展开数组,常用于数组的合并、复制或传递数组作为函数参数。

  • 数组合并

     let arr1 = [1, 2];
     let arr2 = [3, 4];
     let combined = [...arr1, ...arr2]; // [1, 2, 3, 4]
  • 数组复制

     let arr = [1, 2, 3];
     let copy = [...arr]; // [1, 2, 3]
  • 传递数组作为函数参数

     function sum(a, b, c) {
       return a + b + c;
     }
     let arr = [1, 2, 3];
     console.log(sum(...arr)); // 6

Boolean对象

  • toString():将布尔值转换为字符串。

    const bool = true;
    console.log(bool.toString()); // 输出: "true"
  • valueOf():返回布尔值的原始值。

    const bool = new Boolean(true);
    console.log(bool.valueOf()); // 输出: true

Date 对象

1. 获取日期和时间的方法

  • getFullYear(): 返回年份(4 位数)。

    let year = new Date().getFullYear(); // 例如 2023
  • getMonth(): 返回月份(0 到 11,0 表示 1 月)。

    let month = new Date().getMonth(); // 例如 9(表示 10 月)
  • getDate(): 返回日期(1 到 31)。

    let day = new Date().getDate(); // 例如 1
  • getDay(): 返回星期几(0 到 6,0 表示星期日)。

    let weekday = new Date().getDay(); // 例如 0(表示星期日)
  • getHours(): 返回小时(0 到 23)。

    let hours = new Date().getHours(); // 例如 12
  • getMinutes(): 返回分钟(0 到 59)。

    let minutes = new Date().getMinutes(); // 例如 30
  • getSeconds(): 返回秒数(0 到 59)。

    let seconds = new Date().getSeconds(); // 例如 45
  • getMilliseconds(): 返回毫秒数(0 到 999)。

    let milliseconds = new Date().getMilliseconds(); // 例如 123
  • getTime(): 返回从 1970 年 1 月 1 日(UTC)到当前日期的毫秒数。

    let timestamp = new Date().getTime(); // 例如 1696156800000
  • getTimezoneOffset(): 返回当前时区与 UTC 的分钟差。

    let offset = new Date().getTimezoneOffset(); // 例如 -480(表示 UTC+8)

2. 设置日期和时间的方法

  • setFullYear(year): 设置年份。

    let date = new Date();
    date.setFullYear(2024);
  • setMonth(month): 设置月份(0 到 11)。

    let date = new Date();
    date.setMonth(11); // 设置为 12 月
  • setDate(day): 设置日期(1 到 31)。

    let date = new Date();
    date.setDate(25); // 设置为 25 号
  • setHours(hours): 设置小时(0 到 23)。

    let date = new Date();
    date.setHours(15); // 设置为 15 点
  • setMinutes(minutes): 设置分钟(0 到 59)。

    let date = new Date();
    date.setMinutes(45); // 设置为 45 分
  • setSeconds(seconds): 设置秒数(0 到 59)。

    let date = new Date();
    date.setSeconds(30); // 设置为 30 秒
  • setMilliseconds(milliseconds): 设置毫秒数(0 到 999)。

    let date = new Date();
    date.setMilliseconds(500); // 设置为 500 毫秒
  • setTime(timestamp): 通过时间戳设置日期和时间。

    let date = new Date();
    date.setTime(1633072800000); // 设置为 2021-10-01T12:00:00.000Z

3. 日期格式化方法

  • toDateString(): 返回日期的可读字符串(忽略时间)。

    let date = new Date();
    console.log(date.toDateString()); // 例如 "Sun Oct 01 2023"
  • toTimeString(): 返回时间的可读字符串(忽略日期)。

    let date = new Date();
    console.log(date.toTimeString()); // 例如 "12:00:00 GMT+0800 (中国标准时间)"
  • toISOString(): 返回 ISO 格式的日期字符串(UTC 时间)。

    let date = new Date();
    console.log(date.toISOString()); // 例如 "2023-10-01T04:00:00.000Z"
  • toLocaleDateString(): 返回本地格式的日期字符串。

    let date = new Date();
    console.log(date.toLocaleDateString()); // 例如 "2023/10/1"
  • toLocaleTimeString(): 返回本地格式的时间字符串。

    let date = new Date();
    console.log(date.toLocaleTimeString()); // 例如 "12:00:00"
  • toLocaleString(): 返回本地格式的日期和时间字符串。

    let date = new Date();
    console.log(date.toLocaleString()); // 例如 "2023/10/1 12:00:00"

4. 静态方法

  • Date.now(): 返回当前时间的时间戳(毫秒数)。

    let timestamp = Date.now(); // 例如 1696156800000
  • Date.parse(dateString): 解析日期字符串并返回时间戳。

    let timestamp = Date.parse("2023-10-01T12:00:00Z"); // 例如 1696156800000

Math 对象

1. 数学常量

  • Math.PI: 圆周率 π 的值(约 3.14159)。

    console.log(Math.PI); // 3.141592653589793
  • Math.E: 自然对数的底数 e 的值(约 2.71828)。

    console.log(Math.E); // 2.718281828459045
  • Math.LN2: 2 的自然对数(约 0.693)。

    console.log(Math.LN2); // 0.6931471805599453
  • Math.LN10: 10 的自然对数(约 2.302)。

    console.log(Math.LN10); // 2.302585092994046
  • Math.LOG2E: 以 2 为底 e 的对数(约 1.442)。

    console.log(Math.LOG2E); // 1.4426950408889634
  • Math.LOG10E: 以 10 为底 e 的对数(约 0.434)。

    console.log(Math.LOG10E); // 0.4342944819032518
  • Math.SQRT2: 2 的平方根(约 1.414)。

    console.log(Math.SQRT2); // 1.4142135623730951
  • Math.SQRT1_2: 1/2 的平方根(约 0.707)。

    console.log(Math.SQRT1_2); // 0.7071067811865476

2. 数学方法

2.1 取整方法
  • Math.round(x): 四舍五入到最接近的整数。

    console.log(Math.round(4.7)); // 5
    console.log(Math.round(4.3)); // 4
  • Math.ceil(x): 向上取整。

    console.log(Math.ceil(4.1)); // 5
  • Math.floor(x): 向下取整。

    console.log(Math.floor(4.9)); // 4
  • Math.trunc(x): 去除小数部分,返回整数部分。

    console.log(Math.trunc(4.9)); // 4
2.2 最大值和最小值
  • Math.max(x1, x2, ..., xn): 返回一组数中的最大值。

    console.log(Math.max(1, 3, 2)); // 3
  • Math.min(x1, x2, ..., xn): 返回一组数中的最小值。

    console.log(Math.min(1, 3, 2)); // 1
2.3 随机数
  • Math.random(): 返回一个 0 到 1 之间的伪随机数(包括 0,不包括 1)。

    console.log(Math.random()); // 例如 0.123456789
2.4 幂和开方
  • Math.pow(x, y): 返回 x 的 y 次幂。

    console.log(Math.pow(2, 3)); // 8
  • Math.sqrt(x): 返回 x 的平方根。

    console.log(Math.sqrt(16)); // 4
  • Math.cbrt(x): 返回 x 的立方根。

    console.log(Math.cbrt(27)); // 3
  • Math.exp(x): 返回 e 的 x 次幂。

    console.log(Math.exp(1)); // 2.718281828459045
  • Math.expm1(x): 返回 e 的 x 次幂减 1。

    console.log(Math.expm1(1)); // 1.718281828459045
  • Math.hypot(x1, x2, ..., xn): 返回所有参数的平方和的平方根。

    console.log(Math.hypot(3, 4)); // 5
2.5 对数和指数
  • Math.log(x): 返回 x 的自然对数(以 e 为底)。

    console.log(Math.log(Math.E)); // 1
  • Math.log10(x): 返回 x 的以 10 为底的对数。

    console.log(Math.log10(100)); // 2
  • Math.log2(x): 返回 x 的以 2 为底的对数。

    console.log(Math.log2(8)); // 3
  • Math.log1p(x): 返回 1 + x 的自然对数。

    console.log(Math.log1p(1)); // 0.6931471805599453
2.6 三角函数
  • Math.sin(x): 返回 x 的正弦值(x 为弧度)。

    console.log(Math.sin(Math.PI / 2)); // 1
  • Math.cos(x): 返回 x 的余弦值(x 为弧度)。

    console.log(Math.cos(Math.PI)); // -1
  • Math.tan(x): 返回 x 的正切值(x 为弧度)。

    console.log(Math.tan(Math.PI / 4)); // 1
  • Math.asin(x): 返回 x 的反正弦值(结果为弧度)。

    console.log(Math.asin(1)); // 1.5707963267948966 (π/2)
  • Math.acos(x): 返回 x 的反余弦值(结果为弧度)。

    console.log(Math.acos(0)); // 1.5707963267948966 (π/2)
  • Math.atan(x): 返回 x 的反正切值(结果为弧度)。

    console.log(Math.atan(1)); // 0.7853981633974483 (π/4)
  • Math.atan2(y, x): 返回点 (x, y) 与原点连线与 x 轴正方向的夹角(结果为弧度)。

    console.log(Math.atan2(1, 1)); // 0.7853981633974483 (π/4)
2.7 双曲函数
  • Math.sinh(x): 返回 x 的双曲正弦值。

    console.log(Math.sinh(0)); // 0
  • Math.cosh(x): 返回 x 的双曲余弦值。

    console.log(Math.cosh(0)); // 1
  • Math.tanh(x): 返回 x 的双曲正切值。

    console.log(Math.tanh(0)); // 0
2.8 其他方法
  • Math.abs(x): 返回 x 的绝对值。

    console.log(Math.abs(-5)); // 5
  • Math.sign(x): 返回 x 的符号(1 为正,-1 为负,0 为 0,-0 为 -0,NaN 为 NaN)。

    console.log(Math.sign(-10)); // -1
  • Math.clz32(x): 返回 x 的 32 位二进制表示中前导零的个数。

    console.log(Math.clz32(1)); // 31
  • Math.imul(x, y): 返回 x 和 y 的 32 位整数乘法的结果。

    console.log(Math.imul(2, 3)); // 6
  • Math.fround(x): 返回最接近 x 的 32 位单精度浮点数。

    console.log(Math.fround(1.337)); // 1.3370000123977661

Number 对象

1. Number 对象的实例方法

Number 对象继承了 Object 的方法,但它本身也有一些实例方法:

  • toString([radix]): 将数字转换为字符串,可以指定进制(2 到 36)。

    let num = 255;
    console.log(num.toString(16)); // "ff"
  • toFixed(digits): 将数字转换为字符串,保留指定的小数位数。

    let num = 3.14159;
    console.log(num.toFixed(2)); // "3.14"
  • toExponential(digits): 将数字转换为指数形式的字符串,保留指定的小数位数。

    let num = 12345;
    console.log(num.toExponential(2)); // "1.23e+4"
  • toPrecision(precision): 将数字转换为指定精度的字符串。

    let num = 123.456;
    console.log(num.toPrecision(5)); // "123.46"
  • valueOf(): 返回 Number 对象的原始值。

    let numObj = new Number(123);
    console.log(numObj.valueOf()); // 123

2. Number 的静态属性

Number 对象提供了一些静态属性,表示特殊的数字值:

  • Number.MAX_VALUE: JavaScript 中可表示的最大正数(约 1.7976931348623157e+308)。

    console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
  • Number.MIN_VALUE: JavaScript 中可表示的最小正数(约 5e-324)。

    console.log(Number.MIN_VALUE); // 5e-324
  • Number.MAX_SAFE_INTEGER: JavaScript 中可表示的最大安全整数(2^53 - 1,即 9007199254740991)。

    console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
  • Number.MIN_SAFE_INTEGER: JavaScript 中可表示的最小安全整数(-2^53 + 1,即 -9007199254740991)。

    console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
  • Number.POSITIVE_INFINITY: 正无穷大(Infinity)。

    console.log(Number.POSITIVE_INFINITY); // Infinity
  • Number.NEGATIVE_INFINITY: 负无穷大(-Infinity)。

    console.log(Number.NEGATIVE_INFINITY); // -Infinity
  • Number.NaN: 表示非数字(NaN)。

    console.log(Number.NaN); // NaN
  • Number.EPSILON: 表示 1 与大于 1 的最小浮点数之间的差(约 2.220446049250313e-16)。

    console.log(Number.EPSILON); // 2.220446049250313e-16

3. Number 的静态方法

Number 对象提供了一些静态方法,用于处理数字:

  • Number.isFinite(value): 判断一个值是否为有限数。

    console.log(Number.isFinite(123)); // true
    console.log(Number.isFinite(Infinity)); // false
  • Number.isInteger(value): 判断一个值是否为整数。

    console.log(Number.isInteger(123)); // true
    console.log(Number.isInteger(123.456)); // false
  • Number.isNaN(value): 判断一个值是否为 NaN(比全局 isNaN() 更严格)。

    console.log(Number.isNaN(NaN)); // true
    console.log(Number.isNaN("abc")); // false
  • Number.isSafeInteger(value): 判断一个值是否为安全整数(在 Number.MIN_SAFE_INTEGER 和 Number.MAX_SAFE_INTEGER 之间)。

    console.log(Number.isSafeInteger(9007199254740991)); // true
    console.log(Number.isSafeInteger(9007199254740992)); // false
  • Number.parseFloat(string): 将字符串解析为浮点数(与全局 parseFloat() 相同)。

    console.log(Number.parseFloat("123.45")); // 123.45
  • Number.parseInt(string, [radix]): 将字符串解析为整数(与全局 parseInt() 相同)。

    console.log(Number.parseInt("1010", 2)); // 10
  • Number.prototype.toLocaleString([locales, options]): 返回本地化格式的数字字符串。

    let num = 123456.789;
    console.log(num.toLocaleString('en-US')); // "123,456.789"

String 对象

1. String 对象的实例方法

1.1 获取字符串信息
  • length: 返回字符串的长度。

    let str = "hello";
    console.log(str.length); // 5
  • charAt(index): 返回指定索引处的字符。

    let str = "hello";
    console.log(str.charAt(1)); // "e"
  • charCodeAt(index): 返回指定索引处字符的 Unicode 编码。

    let str = "hello";
    console.log(str.charCodeAt(1)); // 101
  • codePointAt(index): 返回指定索引处字符的 Unicode 码点。

    let str = "😊";
    console.log(str.codePointAt(0)); // 128522
1.2 查找和匹配
  • indexOf(searchValue, [fromIndex]): 返回指定值第一次出现的索引,未找到则返回 -1。

    let str = "hello world";
    console.log(str.indexOf("o")); // 4
  • lastIndexOf(searchValue, [fromIndex]): 返回指定值最后一次出现的索引,未找到则返回 -1。

    let str = "hello world";
    console.log(str.lastIndexOf("o")); // 7
  • includes(searchValue, [fromIndex]): 判断字符串是否包含指定值,返回布尔值。

    let str = "hello world";
    console.log(str.includes("world")); // true
  • startsWith(searchValue, [fromIndex]): 判断字符串是否以指定值开头,返回布尔值。

    let str = "hello world";
    console.log(str.startsWith("hello")); // true
  • endsWith(searchValue, [fromIndex]): 判断字符串是否以指定值结尾,返回布尔值。

    let str = "hello world";
    console.log(str.endsWith("world")); // true
  • match(regexp): 返回字符串中与正则表达式匹配的结果。

    let str = "hello world";
    console.log(str.match(/o/g)); // ["o", "o"]
  • search(regexp): 返回字符串中与正则表达式匹配的第一个索引,未找到则返回 -1。

    let str = "hello world";
    console.log(str.search(/world/)); // 6
1.3 截取和分割
  • slice(startIndex, [endIndex]): 返回从 startIndex 到 endIndex(不包括)的子字符串。

    let str = "hello world";
    console.log(str.slice(0, 5)); // "hello"
  • substring(startIndex, [endIndex]): 类似于 slice(),但不支持负数索引。

    let str = "hello world";
    console.log(str.substring(6, 11)); // "world"
  • substr(startIndex, [length]): 返回从 startIndex 开始的指定长度的子字符串(已弃用,建议使用 slice())。

    let str = "hello world";
    console.log(str.substring(6, 11)); // "world"
  • split(separator, [limit]): 将字符串按指定分隔符拆分为数组。

    let str = "hello,world";
    console.log(str.split(",")); // ["hello", "world"]
1.4 替换和修改
  • replace(searchValue, replaceValue): 替换字符串中的指定值。

    let str = "hello world";
    console.log(str.replace("world", "there")); // "hello there"
  • replaceAll(searchValue, replaceValue): 替换字符串中所有匹配的指定值。

    let str = "hello world world";
    console.log(str.replaceAll("world", "there")); // "hello there there"
  • toLowerCase(): 将字符串转换为小写。

    let str = "HELLO";
    console.log(str.toLowerCase()); // "hello"
  • toUpperCase(): 将字符串转换为大写。

    let str = "hello";
    console.log(str.toUpperCase()); // "HELLO"
  • trim(): 去除字符串两端的空白字符。

    let str = "  hello  ";
    console.log(str.trim()); // "hello"
  • trimStart(): 去除字符串开头的空白字符。

    let str = "  hello  ";
    console.log(str.trimStart()); // "hello  "
  • trimEnd(): 去除字符串结尾的空白字符。

    let str = "  hello  ";
    console.log(str.trimEnd()); // "  hello"
  • padStart(targetLength, [padString]): 在字符串开头填充指定字符,直到达到目标长度。

    let str = "123";
    console.log(str.padStart(5, "0")); // "00123"
  • padEnd(targetLength, [padString]): 在字符串结尾填充指定字符,直到达到目标长度。

    let str = "123";
    console.log(str.padEnd(5, "0")); // "12300"
1.5 其他方法
  • concat(str1, str2, ..., strN): 连接多个字符串,返回新字符串。

    let str = "hello";
    console.log(str.concat(" ", "world")); // "hello world"
  • repeat(count): 返回重复指定次数的字符串。

    let str = "hello";
    console.log(str.repeat(2)); // "hellohello"
  • localeCompare(compareString, [locales, options]): 比较两个字符串的本地化顺序。

    let str = "apple";
    console.log(str.localeCompare("banana")); // -1(表示 "apple" 在 "banana" 之前)

2. String 的静态方法

  • String.fromCharCode(code1, code2, ..., codeN): 将 Unicode 编码转换为字符串。

    console.log(String.fromCharCode(72, 69, 76, 76, 79)); // "HELLO"
  • String.fromCodePoint(codePoint1, codePoint2, ..., codePointN): 将 Unicode 码点转换为字符串。

    console.log(String.fromCodePoint(128522)); // "😊"
  • String.raw(): 返回原始模板字符串(不转义反斜杠

    console.log(String.raw`Hello\nWorld`); // "Hello\\nWorld"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值