TypeScript字符串的常用操作總結(jié)
在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)文章
JavaScript高級(jí)程序設(shè)計(jì)閱讀筆記(十六) javascript檢測(cè)瀏覽器和操作系統(tǒng)-detect.js
javascript檢測(cè)瀏覽器和操作系統(tǒng) detect.js使用介紹,需要的朋友可以參考下2012-08-08PHP使用方法重載實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建屬性的get和set方法
這篇文章主要介紹了PHP使用方法重載實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建屬性的get和set方法,使用本文方法可以在一個(gè)類(lèi)中不用在寫(xiě)大量的set方法或get方法,需要的朋友可以參考下2014-11-11JavaScript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器案例
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下<BR>2022-07-07jsonp格式前端發(fā)送和后臺(tái)接受寫(xiě)法的代碼詳解
jsonp是ajax提交的一種格式不會(huì)受跨域限制,這篇文章主要介紹了jsonp格式前端發(fā)送和后臺(tái)接受寫(xiě)法的代碼詳解,需要的朋友可以參考下2019-11-11JavaScript實(shí)現(xiàn)簡(jiǎn)單圖片輪播效果
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單圖片輪播效果,點(diǎn)擊下標(biāo)切換到該圖片上,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08