字符串格式化方法

String.prototype.toLowerCase

1. 语法

str.toLowerCase()

返回值:

小写的字符串。

2. 描述

描述即返回值。

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

  • 用于小写字符串。

3. 示例

  • using toLowerCase

    'ALPHABET'.toLowerCase(); // 'alphabet'
    
    1

String.prototype.toString

1. 语法

str.toString()

返回值:

字符串。

2. 描述

描述即返回值。

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

  • 尚未使用过。

3. 示例

  • using toString

    const x = new String("Hello world");
    x.toString(); // 输出 "Hello world"
    
    1
    2

String.prototype.toUpperCase

1. 语法

str.toUpperCase()

返回值:

大写的字符串。

2. 描述

描述即返回值。

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

  • 常用于大写字符串。

3. 示例

  • using toUpperCase

    'alphabet'.toUpperCase(); // 'ALPHABET'
    
    1

String.prototype.trim

1. 语法

str.trim()

返回值:

两端去掉空白的新字符串。

2. 描述

描述即返回值。

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

  • 尚未使用过。

3. 示例

  • using trim

    const orig = '   foo  ';
    orig.trim(); // 'foo'
    
    1
    2

String.prototype.trimStart

1. 语法

str.trimStart()

返回值:

左侧去掉空白的新字符串。

2. 描述

描述即返回值。

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

  • 尚未使用过。

3. 示例

  • using trimLeft/trimStart

    const str = "   foo  ";
    str.trimStart(); // 'foo  '
    str.trimLeft(); // 'foo  '
    
    1
    2
    3

String.prototype.trimEnd

1. 语法

str.trimEnd()

返回值:

右侧去掉空白的新字符串。

2. 描述

描述即返回值。

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

  • 尚未使用过。

3. 示例

  • using trimEnd

    const str = "   foo  ";
    str.trimRight(); // '   foo'
    str.trimEnd(); // '   foo'
    
    1
    2
    3
最后更新时间: 1 年前