偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

十個(gè)很棒的 JavaScript 字符串技巧

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

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

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

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

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

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


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

2.如何將字符串填充到指定長(zhǎng)度

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

以前主要用庫(kù)left-pad。但是,今天我們可以使用 padStart 和 SpadEnd 方法,選擇取決于字符串是在字符串的開(kāi)頭還是結(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.如何將一個(gè)字符串分割成一個(gè)字符數(shù)組

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

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

4.如何計(jì)算字符串中的字符

可以使用長(zhǎng)度屬性。

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.如何將字符串中的第一個(gè)字母大寫

一個(gè)非常常見(jiàn)的操作是將字符串的首字母大寫,雖然許多編程語(yǔ)言都有一種原生的方式來(lái)做到這一點(diǎn),但 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.如何在多個(gè)分隔符上拆分字符串

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

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

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

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

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

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

要在字符串的開(kāi)頭或結(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 方法,請(qǐng)注意,此新方法并非在所有瀏覽器和 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é)

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

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

2023-04-17 16:19:32

編程語(yǔ)言JavaScript開(kāi)發(fā)

2020-12-31 07:56:02

JavaScript 字符串技巧

2023-11-27 16:01:59

JavaScrip技巧

2025-03-18 07:20:00

JavaScript開(kāi)發(fā)字符串

2024-05-16 11:09:40

Python字符串代碼

2023-10-16 07:55:15

JavaScript對(duì)象技巧

2024-03-04 16:32:02

JavaScript運(yùn)算符

2024-12-02 14:28:17

JavaScriptWeb開(kāi)發(fā)

2023-07-24 07:11:43

2022-08-28 19:03:18

JavaScript編程語(yǔ)言開(kāi)發(fā)

2024-12-03 14:33:42

Python遞歸編程

2023-05-16 15:32:45

JavaScriptWeb前端工程師

2022-06-08 10:42:34

ReduceJavaScript技巧

2022-11-25 14:55:43

JavaScriptweb應(yīng)用程序

2022-04-26 18:33:02

JavaScript技巧代碼

2015-08-24 09:12:00

Redis 技巧

2023-07-02 14:21:06

PythonMatplotlib數(shù)據(jù)可視化庫(kù)

2024-03-17 20:01:51

2021-10-09 10:50:30

JavaScript編程開(kāi)發(fā)

2022-10-20 15:12:43

JavaScript技巧開(kāi)發(fā)
點(diǎn)贊
收藏

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