十个很棒的 JavaScript 字符串技巧

字符串是几乎所有编程语言中的基本类型之一。以下10 个重要的JS技巧可能是你不知道的。
首页 新闻资讯 行业资讯 十个很棒的 JavaScript 字符串技巧

字符串是几乎所有编程语言中的基本类型之一。以下10 个重要的JS技巧可能是你不知道的。

d133367039f4e7faa3b50644b55e22204d2902.jpg

那么,我们现在就开始吧。

1.如何多次复制一个字符串

JS 字符串允许简单的重复,不同于纯手工复制字符串,我们可以使用字符串重复的方法。

复制

const laughing = 'Maxwell '.repeat(3)consol.log(laughing) // "Maxwell Maxwell Maxwell "const eightBits = '1'.repeat(8)console.log(eightBits) // "11111111"
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

2.如何将字符串填充到指定长度

有时我们希望字符串具有特定的长度。如果字符串太短,则需要填充剩余空间,直到达到指定长度。

以前主要用库left-pad。但是,今天我们可以使用 padStart 和 SpadEnd 方法,选择取决于字符串是在字符串的开头还是结尾填充。

复制

// Add "0" to the beginning until the length of the string is 8.const eightBits = '001'.padStart(8, '0')console.log(eightBits) // "00000001"//Add " *" at the end until the length of the string is 5.const anonymizedCode = "34".padEnd(5, "*")console.log(anonymizedCode) // "34***"
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

3.如何将一个字符串分割成一个字符数组

有几种方法可以将字符串拆分为字符数组,我更喜欢使用扩展运算符 (...) :

复制

const word = 'Maxwell'
 const characters = [...word] console.log(characters)
  • 1.

  • 2.

  • 3.

4.如何计算字符串中的字符

可以使用长度属性。

复制

const word = "apple";console.log(word.length) // 5
  • 1.

  • 2.

5.如何反转字符串中的字符

反转字符串中的字符很容易,只需组合扩展运算符 (...)、Array.reverse 方法和 Array.join 方法。

复制

const word = "apple"const reversedWord = [...word].reverse().join("")console.log(reversedWord) // "elppa"
  • 1.

  • 2.

  • 3.

6.如何将字符串中的第一个字母大写

一个非常常见的操作是将字符串的首字母大写,虽然许多编程语言都有一种原生的方式来做到这一点,但 JS 需要做一些工作。

复制

let word = 'apply'word = word[0].toUpperCase() + word.substr(1)console.log(word) // "Apple"
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

另一种方法:

复制

// This shows an alternative waylet word = "apple";const characters = [...word];characters[0] = characters[0].toUpperCase();word = characters.join("");console.log(word); // "Apple"
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

7.如何在多个分隔符上拆分字符串

假设我们要在一个分隔符上拆分一个字符串,我们首先想到的就是使用split方法,这个方法当然是聪明人都知道的。但是,你可能不知道的一点是,split 可以同时拆分多个定界符,这可以通过使用正则表达式来实现。

复制

const list = "apples,bananas;cherries"const fruits = list.split(/[,;]/)console.log(fruits); // ["apples", "bananas", "cherries"]
  • 1.

  • 2.

  • 3.

8. 如何检查字符串是否包含特定序列

字符串搜索是一项常见任务,在 JS 中,你可以使用 String.includes 方法轻松完成此操作,不需要正则表达式。

复制

const text = "Hello, world! My name is Kai!"console.log(text.includes("Kai")); // true
  • 1.

  • 2.

9. 如何检查字符串是否以特定序列开始或结束

要在字符串的开头或结尾搜索,可以使用 String.startsWith 和 String.endsWith 方法。

复制

const text = "Hello, world! My name is Kai!"console.log(text.startsWith("Hello")); // trueconsole.log(text.endsWith("world")); // false
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

10.如何替换所有出现的字符串

有多种方法可以替换所有出现的字符串,您可以使用 String.replace 方法和带有全局标志的正则表达式;或者使用新的 String.replaceAll 方法,请注意,此新方法并非在所有浏览器和 Node.js 版本中都可用。

复制

const text = "I like apples. You like apples."console.log(text.replace(/apples/g, "bananas"));// "I like bananas. You like bananas."console.log(text.replaceAll("apples", "bananas"));
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

总结

字符串是几乎所有编程语言中最基本的数据类型之一。此外,它是新开发人员最先学习的数据类型之一。然而,尤其是在 JavaScript 中,许多开发人员并不知道有关字符串的一些有趣细节