TypeScript字符串的常用操作總結
在TypeScript中,字符串的常用操作可以使用以下方法來實現(xiàn):
常用
substring(startIndex: number, endIndex?: number): string:返回從startIndex開始到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開始到endIndex(不包括)之間的子字符串。如果省略endIndex,則返回從startIndex到字符串末尾的子字符串。與substring()方法類似,但slice()方法也支持負數(shù)索引。
const str = "Hello, World!"; const subStr = str.slice(7, 12); // "World"
replace(searchValue: string | RegExp, replaceValue: string): string:將字符串中的searchValue替換為replaceValue,并返回新的字符串。searchValue可以是一個字符串或正則表達式。
const str = "Hello, World!"; const newStr = str.replace("World", "Universe"); // "Hello, Universe!"
toUpperCase(): string:將字符串轉換為大寫。
const str = "Hello, World!"; const upperCaseStr = str.toUpperCase(); // "HELLO, WORLD!"
toLowerCase(): string:將字符串轉換為小寫。
const str = "Hello, World!"; const lowerCaseStr = str.toLowerCase(); // "hello, world!"
trim(): string:去除字符串兩端的空格。
const str = " Hello, World! "; const trimmedStr = str.trim(); // "Hello, World!"
這些方法是字符串處理中常用的操作,可以根據(jù)具體的需求選擇適合的方法來處理字符串。需要注意的是,這些方法都返回新的字符串,原始字符串并不會被修改。
查找字符串
在JavaScript/TypeScript中,有多種方法可以用于查找字符串。以下是幾種常見的方法:
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:使用正則表達式搜索字符串,并返回第一個匹配的索引位置。如果找不到匹配項,則返回-1。
const str = "Hello, World!"; const index = str.search(/World/); // 7
includes(searchValue: string, startIndex?: number): boolean:判斷字符串中是否包含searchValue。如果包含,則返回true,否則返回false??梢允褂胹tartIndex參數(shù)指定搜索的起始位置。
const str = "Hello, World!"; const includes = str.includes("World"); // true
startsWith(searchValue: string, startIndex?: number): boolean:判斷字符串是否以searchValue開頭。如果是,則返回true,否則返回false。可以使用startIndex參數(shù)指定搜索的起始位置。
const str = "Hello, World!"; const startsWith = str.startsWith("Hello"); // true
endsWith(searchValue: string, endIndex?: number): boolean:判斷字符串是否以searchValue結尾。如果是,則返回true,否則返回false。可以使用endIndex參數(shù)指定搜索的結束位置。
const str = "Hello, World!"; const endsWith = str.endsWith("World"); // false
以上是一些常用的字符串查找方法,根據(jù)具體的需求選擇適合的方法來查找字符串。需要注意的是,這些方法都返回布爾值或索引位置,而不是具體的匹配字符串。
提取字符串
在JavaScript/TypeScript中,有多種方法可以用于提取字符串的子串。以下是幾種常見的方法:
substring(startIndex: number, endIndex?: number): string:返回從startIndex開始到endIndex(不包括)之間的子字符串。如果省略endIndex,則返回從startIndex到字符串末尾的子字符串。與slice()方法類似,但substring()方法不支持負數(shù)索引。
const str = "Hello, World!"; const subStr = str.substring(7, 12); // "World"
substr(startIndex: number, length?: number): string:返回從startIndex開始,長度為length的子字符串。如果省略length,則返回從startIndex到字符串末尾的子字符串。
const str = "Hello, World!"; const subStr = str.substr(7, 5); // "World"
slice(startIndex: number, endIndex?: number): string:返回從startIndex開始到endIndex(不包括)之間的子字符串。如果省略endIndex,則返回從startIndex到字符串末尾的子字符串。與substring()方法類似,但slice()方法也支持負數(shù)索引。
const str = "Hello, World!"; const subStr = str.slice(7, 12); // "World"
split(separator: string | RegExp, limit?: number): string[]:將字符串分割成子字符串數(shù)組,根據(jù)指定的分隔符separator進行分割??梢允褂胠imit參數(shù)限制返回的子字符串數(shù)量。
const str = "Hello, World!"; const parts = str.split(","); // ["Hello", " World!"]
這些方法可以根據(jù)具體的需求選擇適合的方法來提取字符串的子串。需要注意的是,這些方法返回新的字符串或字符串數(shù)組,原始字符串并不會被修改。
到此這篇關于TypeScript字符串的常用操作總結的文章就介紹到這了,更多相關TypeScript字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JavaScript高級程序設計閱讀筆記(十六) javascript檢測瀏覽器和操作系統(tǒng)-detect.js
javascript檢測瀏覽器和操作系統(tǒng) detect.js使用介紹,需要的朋友可以參考下2012-08-08PHP使用方法重載實現(xiàn)動態(tài)創(chuàng)建屬性的get和set方法
這篇文章主要介紹了PHP使用方法重載實現(xiàn)動態(tài)創(chuàng)建屬性的get和set方法,使用本文方法可以在一個類中不用在寫大量的set方法或get方法,需要的朋友可以參考下2014-11-11