如何在TypeScript中獲取子字串?
字串包含各種字元,子字串是字串的一部分。我們可以透過兩種方式獲取字串的子字串。第一種是使用子字串的起始和結束位置,第二種是使用子字串的起始位置和長度。
在本教程中,我們將學習在TypeScript中使用這兩種方法從字串中獲取子字串。
使用起始位置和結束位置獲取子字串
在TypeScript中,字串的索引從零開始。因此,如果我們有子字串的基於零的起始和結束位置,我們可以從特定字串中獲取子字串。
語法
使用者可以按照以下語法使用起始和結束位置從特定字串中獲取子字串。
get_sub_string(start, end): string {
let resultantString = "";
for (let i = 0; i < this.string.length; i++) {
if (i >= start && i <= end) {
resultantString += this.string[i];
}
}
return resultantString;
}
演算法
步驟1 − 定義get_sub_string()方法,該方法接受子字串的起始和結束索引並返回字串。
步驟2 − 建立型別為字串的resultantString變數以儲存子字串。
步驟3 − 使用for迴圈遍歷字串,我們需要從中獲取子字串。
步驟4 − 在for迴圈內,檢查索引是否在包括起始和結束位置之間,然後將該位置的字元附加到resultantString變數。
步驟5 − for迴圈迭代完成後,返回resultantString,這就是所需的子字串。
示例
在下面的示例中,我們建立了包含字串成員的stringClass類。此外,我們根據上述語法和演算法實現了get_sub_string()方法。
之後,我們建立了stringClass的物件並透過傳遞不同的起始和結束位置作為引數來呼叫get_sub_sting()方法。
// Creating a class named stringClass
class stringClass {
// string memeber
string: string;
// constructor to initialize the string member
constructor(str: string) {
this.string = str;
}
// method to get substring, which takes the start and end property
get_sub_string(start: number, end: number): string {
// defining the resultantString variable to store substring
let resultantString = "";
for (let i = 0; i < this.string.length; i++) {
// if index is between start, and end, include the character of that position to resultantString
if (i >= start && i <= end) {
resultantString += this.string[i];
}
}
// return the substring
return resultantString;
}
}
let str1: stringClass = new stringClass("TutorialsPoint");
console.log(
"The substring of TutorialsPoint starting from 2 to 5 is " +
str1.get_sub_string(2, 5)
);
console.log(
"The substring of TutorialsPoint starting from 0 to 7 is " +
str1.get_sub_string(0, 7)
);
編譯後,它將生成以下JavaScript程式碼:
// Creating a class named stringClass
var stringClass = /** @class */ (function () {
// constructor to initialize the string member
function stringClass(str) {
this.string = str;
}
// method to get substring, which takes the start and end property
stringClass.prototype.get_sub_string = function (start, end) {
// defining the resultantString variable to store substring
var resultantString = "";
for (var i = 0; i < this.string.length; i++) {
// if index is between start, and end, include the character of that position to resultantString
if (i >= start && i <= end) {
resultantString += this.string[i];
}
}
// return the substring
return resultantString;
};
return stringClass;
}());
var str1 = new stringClass("TutorialsPoint");
console.log("The substring of TutorialsPoint starting from 2 to 5 is " +
str1.get_sub_string(2, 5));
console.log("The substring of TutorialsPoint starting from 0 to 7 is " +
str1.get_sub_string(0, 7));
輸出
上述程式碼將產生以下輸出:
The substring of TutorialsPoint starting from 2 to 5 is tori The substring of TutorialsPoint starting from 0 to 7 is Tutorial
使用substring()方法獲取子字串
與其從頭編寫函式來使用起始和結束位置獲取子字串,不如使用TypeScript中字串類的substring()方法。它的工作原理與上面的示例相同,並返回子字串。
語法
使用者可以按照以下語法在TypeScript中使用substring()方法。
let str: string = "Hello"; let subString: string = str.substring(start,[end]);
引數
start − 這是必需的引數,表示需要從中獲取子字串的起始位置。
end − 這是一個可選引數,直到我們獲取子字串。它不包括結束位置的字元。
返回值
在這個例子中,我們定義了名為sampleString的字串變數。此外,我們使用substring()方法,並以samleString為參考。此外,我們為substring()方法傳遞了不同的起始和結束引數值,使用者可以觀察輸出。
示例
let sampleString: string = "Welcome";
let subString: string = sampleString.substring(0, 4);
console.log("The substring from 0 to 4 is " + subString);
// using substring() method without passing end parameter
subString = sampleString.substring(2);
console.log("The substring from 2 to end is " + subString);
subString = sampleString.substring(-2, 5);
console.log("The substring from -2 to 5 is " + subString);
subString = sampleString.substring(-2, -5);
console.log("The substring from -2 to -5 is " + subString);
subString = sampleString.substring(-5, -2);
console.log("The substring from -5 to -2 is " + subString);
編譯後,它將生成以下JavaScript程式碼:
var sampleString = "Welcome";
var subString = sampleString.substring(0, 4);
console.log("The substring from 0 to 4 is " + subString);
// using substring() method without passing end parameter
subString = sampleString.substring(2);
console.log("The substring from 2 to end is " + subString);
subString = sampleString.substring(-2, 5);
console.log("The substring from -2 to 5 is " + subString);
subString = sampleString.substring(-2, -5);
console.log("The substring from -2 to -5 is " + subString);
subString = sampleString.substring(-5, -2);
console.log("The substring from -5 to -2 is " + subString);
輸出
上述程式碼將產生以下輸出:
The substring from 0 to 4 is Welc The substring from 2 to end is lcome The substring from -2 to 5 is Welco The substring from -2 to -5 is The substring from -5 to -2 is
在上面的輸出中,使用者可以觀察到,如果我們傳遞兩個負值作為引數,substring()方法將返回一個空字串。如果我們只傳遞起始負值,它將從第0個索引開始子字串。此外,如果我們不傳遞結束引數,它將返回從起始位置到字串長度的子字串。
使用substr()方法獲取子字串
在TypeScript中,substr()方法也返回特定字串中的子字串,並且與字串類的substring()方法幾乎相同。substr()和substring()方法之間的基本區別在於它們作為引數所取的值。substring()將結束位置作為第二個引數,而substr()將子字串的長度作為第二個引數。
語法
在下面的語法中,我們使用了substr()方法。
let str2: string = "Hi there!"; let substring: stirng = str2.substr(start, [len])
引數
start − 這是子字串的基於零的起始位置。
len − 這是一個可選引數,指的是子字串的長度。
返回值
substr()方法返回從start開始長度為len的子字串。如果我們不傳遞len引數,它將返回從start到結束的子字串。
示例
在這個例子中,我們使用了不同引數值的substr()方法。使用者可以觀察輸出以及它如何使用不同的start和len引數值返回子字串。
let demoStr: string = "Shubham";
let substring: string = demoStr.substr(1,3);
console.log("The substring of length 3 starting from 1st index is " + substring);
// substr() method without an optional parameter
substring = demoStr.substr(3);
console.log("The substring starting from 3rd index is " + substring);
編譯後,它將生成以下JavaScript程式碼:
var demoStr = "Shubham";
var substring = demoStr.substr(1, 3);
console.log("The substring of length 3 starting from 1st index is " + substring);
// substr() method without an optional parameter
substring = demoStr.substr(3);
console.log("The substring starting from 3rd index is " + substring);
輸出
上述程式碼將產生以下輸出:
The substring of length 3 starting from 1st index is hub The substring starting from 3rd index is bham
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP