欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

TypeScript字符串的常用操作總結(jié)

 更新時(shí)間:2023年08月03日 09:02:54   作者:一花一world  
這篇文章主要為大家詳細(xì)介紹了TypeScript中字符串的常用操作,例如substring、indexOf、slice、replace等,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

在TypeScript中,字符串的常用操作可以使用以下方法來(lái)實(shí)現(xiàn):

常用

substring(startIndex: number, endIndex?: number): string:返回從startIndex開(kāi)始到endIndex(不包括)之間的子字符串。如果省略endIndex,則返回從startIndex到字符串末尾的子字符串。

const str = "Hello, World!";
const subStr = str.substring(7, 12); // "World"

indexOf(searchValue: string, startIndex?: number): number:返回searchValue在字符串中第一次出現(xiàn)的索引位置。如果找不到該值,則返回-1??梢允褂胹tartIndex參數(shù)指定搜索的起始位置。

const str = "Hello, World!";
const index = str.indexOf("World"); // 7

slice(startIndex: number, endIndex?: number): string:返回從startIndex開(kāi)始到endIndex(不包括)之間的子字符串。如果省略endIndex,則返回從startIndex到字符串末尾的子字符串。與substring()方法類(lèi)似,但slice()方法也支持負(fù)數(shù)索引。

const str = "Hello, World!";
const subStr = str.slice(7, 12); // "World"

replace(searchValue: string | RegExp, replaceValue: string): string:將字符串中的searchValue替換為replaceValue,并返回新的字符串。searchValue可以是一個(gè)字符串或正則表達(dá)式。

const str = "Hello, World!";
const newStr = str.replace("World", "Universe"); // "Hello, Universe!"

toUpperCase(): string:將字符串轉(zhuǎn)換為大寫(xiě)。

const str = "Hello, World!";
const upperCaseStr = str.toUpperCase(); // "HELLO, WORLD!"

toLowerCase(): string:將字符串轉(zhuǎn)換為小寫(xiě)。

const str = "Hello, World!";
const lowerCaseStr = str.toLowerCase(); // "hello, world!"

trim(): string:去除字符串兩端的空格。

const str = "   Hello, World!   ";
const trimmedStr = str.trim(); // "Hello, World!"

這些方法是字符串處理中常用的操作,可以根據(jù)具體的需求選擇適合的方法來(lái)處理字符串。需要注意的是,這些方法都返回新的字符串,原始字符串并不會(huì)被修改。

查找字符串

在JavaScript/TypeScript中,有多種方法可以用于查找字符串。以下是幾種常見(jiàn)的方法:

indexOf(searchValue: string, startIndex?: number): number:返回searchValue在字符串中第一次出現(xiàn)的索引位置。如果找不到該值,則返回-1??梢允褂胹tartIndex參數(shù)指定搜索的起始位置。

const str = "Hello, World!";
const index = str.indexOf("World"); // 7

lastIndexOf(searchValue: string, startIndex?: number): number:返回searchValue在字符串中最后一次出現(xiàn)的索引位置。如果找不到該值,則返回-1??梢允褂胹tartIndex參數(shù)指定搜索的起始位置。

const str = "Hello, World!";
const index = str.lastIndexOf("o"); // 8

search(regexp: string | RegExp): number:使用正則表達(dá)式搜索字符串,并返回第一個(gè)匹配的索引位置。如果找不到匹配項(xiàng),則返回-1。

const str = "Hello, World!";
const index = str.search(/World/); // 7

includes(searchValue: string, startIndex?: number): boolean:判斷字符串中是否包含searchValue。如果包含,則返回true,否則返回false。可以使用startIndex參數(shù)指定搜索的起始位置。

const str = "Hello, World!";
const includes = str.includes("World"); // true

startsWith(searchValue: string, startIndex?: number): boolean:判斷字符串是否以searchValue開(kāi)頭。如果是,則返回true,否則返回false。可以使用startIndex參數(shù)指定搜索的起始位置。

const str = "Hello, World!";
const startsWith = str.startsWith("Hello"); // true

endsWith(searchValue: string, endIndex?: number): boolean:判斷字符串是否以searchValue結(jié)尾。如果是,則返回true,否則返回false??梢允褂胑ndIndex參數(shù)指定搜索的結(jié)束位置。

const str = "Hello, World!";
const endsWith = str.endsWith("World"); // false

以上是一些常用的字符串查找方法,根據(jù)具體的需求選擇適合的方法來(lái)查找字符串。需要注意的是,這些方法都返回布爾值或索引位置,而不是具體的匹配字符串。

提取字符串

在JavaScript/TypeScript中,有多種方法可以用于提取字符串的子串。以下是幾種常見(jiàn)的方法:

substring(startIndex: number, endIndex?: number): string:返回從startIndex開(kāi)始到endIndex(不包括)之間的子字符串。如果省略endIndex,則返回從startIndex到字符串末尾的子字符串。與slice()方法類(lèi)似,但substring()方法不支持負(fù)數(shù)索引。

const str = "Hello, World!";
const subStr = str.substring(7, 12); // "World"

substr(startIndex: number, length?: number): string:返回從startIndex開(kāi)始,長(zhǎng)度為length的子字符串。如果省略length,則返回從startIndex到字符串末尾的子字符串。

const str = "Hello, World!";
const subStr = str.substr(7, 5); // "World"

slice(startIndex: number, endIndex?: number): string:返回從startIndex開(kāi)始到endIndex(不包括)之間的子字符串。如果省略endIndex,則返回從startIndex到字符串末尾的子字符串。與substring()方法類(lèi)似,但slice()方法也支持負(fù)數(shù)索引。

const str = "Hello, World!";
const subStr = str.slice(7, 12); // "World"

split(separator: string | RegExp, limit?: number): string[]:將字符串分割成子字符串?dāng)?shù)組,根據(jù)指定的分隔符separator進(jìn)行分割??梢允褂胠imit參數(shù)限制返回的子字符串?dāng)?shù)量。

const str = "Hello, World!";
const parts = str.split(","); // ["Hello", " World!"]

這些方法可以根據(jù)具體的需求選擇適合的方法來(lái)提取字符串的子串。需要注意的是,這些方法返回新的字符串或字符串?dāng)?shù)組,原始字符串并不會(huì)被修改。

到此這篇關(guān)于TypeScript字符串的常用操作總結(jié)的文章就介紹到這了,更多相關(guān)TypeScript字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論