Zlog
首页时间轴关于
Zlog

一个探索技术、编程和构建 Web 的个人空间。

© 2026 Zlog

导航

首页时间轴关于

链接

首页/字符串拼接方法

字符串拼接方法

str.concat(string2, string3[, ..., stringN])

Z

Zephyr110

2019年12月28日·1 分钟
|
frontendfrontend-javascript

String.prototype.concat

1. 语法

“

警告: str.concat(string2, string3[, ..., stringN])

参数:

  • string2...stringN:和原字符串连接的多个字符串。

返回值:

返回一个新的字符串。

2. 描述

描述即返回值。

“

个人在日常开发中常用场景:

  • 一般使用模板字符串或 + 拼接,尚未使用过,强烈不推荐使用该方法。

String.prototype.padStart

1. 语法

“

警告: str.padStart(targetLength [, padString])

参数:

  • targetLength:当前字符串需要填充到的目标长度;
  • padString:填充字符串。

返回值:

在原字符串开头填充指定字符串直到形成直到目标长度字符串。

2. 描述

描述即返回值。

“

个人在日常开发中常用场景:

  • 尚未使用过。

3. 示例

  • using padStart

    js
    'abc'.padStart(10, "foo");  // "foofoofabc"
    'abc'.padStart(6,"123465"); // "123abc"
    'abc'.padStart(8, "0");     // "00000abc"
    'abc'.padStart(1);          // "abc"
    'abc'.padStart(4);          // " abc"
    'abc'.padStart(10);         // "       abc"

String.prototype.padEnd

1. 语法

“

警告: str.padEnd(targetLength [, padString])

参数:

  • targetLength:当前字符串需要填充到的目标长度;
  • padString:填充字符串。

返回值:

在原字符串末尾填充指定字符串直到形成直到目标长度字符串。

2. 描述

与 padStart 一致,在末尾添加。

String.prototype.repeat

1. 语法

“

警告: str.repeat(count)

参数:

  • count:重复的次数。

返回值:

指定数量的新字符串。

2. 描述

描述即返回值。

“

个人在日常开发中常用场景:

  • 填充字符串时使用。

3. 示例

  • using repeat

    js
    'abc'.repeat(-1);   // RangeError
    'abc'.repeat(0);    // ''
    'abc'.repeat(1);    // 'abc'
    'abc'.repeat(2);    // 'abcabc'
    'abc'.repeat(3.5);  // 'abcabcabc' (count will be converted to integer)

标签

frontendfrontend-javascript

评论

相关文章

Facade

外观模式:就是提供一个统一的接口去访问多个子系统的多个不同的接口,为子系统中的一组接口提供统一的高层接口。使得子系统更容易使用,不仅简化类中的接口,而且实现调用者和接口的解耦。

2021年12月26日·2 分钟

Singleton

单例模式:就是在整个运行时域,一个类只有一个实例对象。

2021年12月26日·2 分钟

Function Overloading

函数名相同,函数的参数列表不同(参数个数、参数类型),根据参数的不同之行不同的操作。

2021年12月19日·2 分钟

← 返回文章列表