自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

十個很棒的 JavaScript 字符串技巧

開發(fā) 前端
字符串是幾乎所有編程語言中的基本類型之一。以下10 個重要的JS技巧可能是你不知道的。

字符串是幾乎所有編程語言中的基本類型之一。以下10 個重要的JS技巧可能是你不知道的。

那么,我們現(xiàn)在就開始吧。

1.如何多次復(fù)制一個字符串

JS 字符串允許簡單的重復(fù),不同于純手工復(fù)制字符串,我們可以使用字符串重復(fù)的方法。

const laughing = 'Maxwell '.repeat(3)
consol.log(laughing) // "Maxwell Maxwell Maxwell "


const eightBits = '1'.repeat(8)
console.log(eightBits) // "11111111"

2.如何將字符串填充到指定長度

有時我們希望字符串具有特定的長度。如果字符串太短,則需要填充剩余空間,直到達(dá)到指定長度。

以前主要用庫left-pad。但是,今天我們可以使用 padStart 和 SpadEnd 方法,選擇取決于字符串是在字符串的開頭還是結(jié)尾填充。

// 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***"

3.如何將一個字符串分割成一個字符數(shù)組

有幾種方法可以將字符串拆分為字符數(shù)組,我更喜歡使用擴(kuò)展運(yùn)算符 (...) :

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

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

可以使用長度屬性。

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

5.如何反轉(zhuǎn)字符串中的字符

反轉(zhuǎn)字符串中的字符很容易,只需組合擴(kuò)展運(yùn)算符 (...)、Array.reverse 方法和 Array.join 方法。

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

6.如何將字符串中的第一個字母大寫

一個非常常見的操作是將字符串的首字母大寫,雖然許多編程語言都有一種原生的方式來做到這一點,但 JS 需要做一些工作。

let word = 'apply'


word = word[0].toUpperCase() + word.substr(1)


console.log(word) // "Apple"

另一種方法:

// This shows an alternative way
let word = "apple";




const characters = [...word];
characters[0] = characters[0].toUpperCase();
word = characters.join("");


console.log(word); // "Apple"

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

假設(shè)我們要在一個分隔符上拆分一個字符串,我們首先想到的就是使用split方法,這個方法當(dāng)然是聰明人都知道的。但是,你可能不知道的一點是,split 可以同時拆分多個定界符,這可以通過使用正則表達(dá)式來實現(xiàn)。

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

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

字符串搜索是一項常見任務(wù),在 JS 中,你可以使用 String.includes 方法輕松完成此操作,不需要正則表達(dá)式。

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

9. 如何檢查字符串是否以特定序列開始或結(jié)束

要在字符串的開頭或結(jié)尾搜索,可以使用 String.startsWith 和 String.endsWith 方法。

const text = "Hello, world! My name is Kai!"


console.log(text.startsWith("Hello")); // true


console.log(text.endsWith("world")); // false

10.如何替換所有出現(xiàn)的字符串

有多種方法可以替換所有出現(xiàn)的字符串,您可以使用 String.replace 方法和帶有全局標(biāo)志的正則表達(dá)式;或者使用新的 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"));

總結(jié)

字符串是幾乎所有編程語言中最基本的數(shù)據(jù)類型之一。此外,它是新開發(fā)人員最先學(xué)習(xí)的數(shù)據(jù)類型之一。然而,尤其是在 JavaScript 中,許多開發(fā)人員并不知道有關(guān)字符串的一些有趣細(xì)節(jié)

責(zé)任編輯:華軒 來源: web前端開發(fā)
相關(guān)推薦

2023-04-17 16:19:32

編程語言JavaScript開發(fā)

2020-12-31 07:56:02

JavaScript 字符串技巧

2023-11-27 16:01:59

JavaScrip技巧

2025-03-18 07:20:00

JavaScript開發(fā)字符串

2024-05-16 11:09:40

Python字符串代碼

2023-10-16 07:55:15

JavaScript對象技巧

2024-03-04 16:32:02

JavaScript運(yùn)算符

2024-12-02 14:28:17

JavaScriptWeb開發(fā)

2023-07-24 07:11:43

2022-08-28 19:03:18

JavaScript編程語言開發(fā)

2023-05-16 15:32:45

JavaScriptWeb前端工程師

2022-06-08 10:42:34

ReduceJavaScript技巧

2024-12-03 14:33:42

Python遞歸編程

2025-02-21 12:30:00

字符串前端JavaScript

2022-04-26 18:33:02

JavaScript技巧代碼

2024-03-17 20:01:51

2021-10-09 10:50:30

JavaScript編程開發(fā)

2022-10-20 15:12:43

JavaScript技巧開發(fā)

2022-11-25 14:55:43

JavaScriptweb應(yīng)用程序

2024-01-03 08:53:35

JavaScrip編程語言NodeJS
點贊
收藏

51CTO技術(shù)棧公眾號