如何在TypeScript中替換字串的子字串?


有時,在使用TypeScript時,我們需要用新字串或任何特定字元替換子字串。替換子字串或字串部分的簡單方法是使用replace()方法。

在這裡,我們將建立一個自定義演算法來替換字串,用於初學者的面試目的。但是,我們也將在本教程的最後看到replace()方法。

建立自定義演算法以替換子字串

在本部分中,我們將使用for迴圈迭代主字串。我們將使用字串庫的substr()方法來查詢索引'i'處的子字串是否與可替換字串匹配。如果索引位置'i'處的子字串與可替換字串匹配,我們將用新字串替換它。

語法

在下面的語法中,我們實現了自定義演算法,用新字串替換字串的一部分。我們使用substr()方法查詢舊子字串。

function replaceSubString(
   mainString: string,
   oldString: string,
   newString: string
): string {
   let tempString: string = "";
   for (let i = 0; i < mainString.length; i++) {
      if (mainString.substr(i, oldString.length) === oldString) {
         tempString = tempString + newString;
         i = i + oldString.length-1;
      } else {
         tempString = tempString + mainString[i];
      }
   }
   return tempString;
}

引數

上述自定義函式採用三個字串型別的引數。

  • mainString − 這是我們需要替換字串的字串。

  • oldString − 這是要替換的子字串。

  • newString − 這是將替換oldString的新字串。

演算法

  • 步驟1 − 建立一個函式,該函式採用mainString、oldString和newString作為引數並返回一個字串。

  • 步驟2 − 建立一個tempString變數來儲存替換後的字串。

  • 步驟3 − 使用for迴圈迭代字串。

  • 步驟4 − 使用substr()庫方法從第i個索引獲取與oldString長度相同的子字串。

  • 步驟5 − 將子字串與oldString匹配。如果第i個索引位置的子字串與oldString匹配,則將新字串新增到tempString。

  • 步驟6 − 將'i'變數的值增加oldstring的長度 - 1。

  • 步驟7 − 如果第i個索引處的子字串與oldString不匹配,則將mainString中第i個索引的字元新增到tempString。

  • 步驟8 − 迭代完成後,返回tempString。

示例

在下面的示例中,我們使用了mainString將單個單詞替換為新單詞。我們建立了一個名為replaceSubstring()的自定義函式並實現了上述演算法。

在輸出中,使用者可以觀察到在mainString中,“TypeScript”字被“coding”字替換。

// define the mainString, oldString, newString
let mainString: string =
"TutorialsPoint is the best website to learn TypeScript!";
let oldString: string = "TypeScript";
let newString: string = "Coding";

// function to replace a substring
function replaceSubString(
   mainString: string,
   oldString: string,
   newString: string
): 
string {
   // create a temporary string
   let tempString: string = "";
   // iterate through the string
   for (let i = 0; i < mainString.length; i++) {
      // get the substring from ith index of length same as oldString's length and compare it with the oldString.
      // If it matches with oldString, add new string to tempstring, and increase i by oldString's length-1.
      if (mainString.substr(i, oldString.length) === oldString) {
         tempString = tempString + newString;
         i = i + oldString.length - 1;
      } 
      else {
         // if substring at index ith position doesn't match, add character from ith index to tempString
         tempString = tempString + mainString[i];
      }
   }
   // return tempString after iteration.
   return tempString;
}
// call the replaceSubString function
console.log("The old string is " + mainString);
console.log("The new string is ");
console.log(replaceSubString(mainString, oldString, newString));

編譯後,它將生成以下JavaScript程式碼:

// define the mainString, oldString, newString
var mainString = "TutorialsPoint is the best website to learn TypeScript!";
var oldString = "TypeScript";
var newString = "Coding";
// function to replace a substring
function replaceSubString(mainString, oldString, newString) {
   // create a temporary string
   var tempString = "";
   // iterate through the string
   for (var i = 0; i < mainString.length; i++) {
      // get the substring from ith index of length same as oldString's length and compare it with the oldString.
      // If it matches with oldString, add new string to tempstring, and increase i by oldString's length-1.
      if (mainString.substr(i, oldString.length) === oldString) {
         tempString = tempString + newString;
         i = i + oldString.length - 1;
      } else {
         // if substring at index ith position doesn't match, add character from ith index to tempString
         tempString = tempString + mainString[i];
      }
   }
   // return tempString after iteration.
   return tempString;
}
// call the replaceSubString function
console.log("The old string is " + mainString);
console.log("The new string is ");
console.log(replaceSubString(mainString, oldString, newString));

輸出

上述程式碼將產生以下輸出:

The old string is TutorialsPoint is the best website to learn TypeScript!
The new string is
TutorialsPoint is the best website to learn Coding!

使用replace()方法替換字串的一部分

replace方法允許我們替換字串的正則表示式或特定單詞。此外,我們還可以使用replace()方法替換特定子字串的所有出現。我們需要透過獲取需要進行替換的字串作為引用來呼叫它。

語法

使用者可以按照以下語法用新字串替換字串的特定部分。

str.replace(reg_exp, new_str);

引數

  • reg_exp − 它將正則表示式作為第一個引數來匹配子字串。

  • new_str − 這是用於替換與正則表示式匹配的子字串的新字串。

示例

在這個例子中,我們使用了replace()方法用“sample”字串替換str的特定子字串。在輸出中,使用者可以觀察到“demo”被“sample”替換。

// defining the string
let str: string = "This is a demo string!";

// use the replace method to replace the "demo" substring with "sample"
let newString: string = str.replace("demo", "sample");
console.log("The old string is " + str);
console.log("The new String is " + newString);

編譯後,它將生成以下JavaScript程式碼:

// defining the string
var str = "This is a demo string!";

// use the replace method to replace the "demo" substring with "sample"
var newString = str.replace("demo", "sample");
console.log("The old string is " + str);
console.log("The new String is " + newString);

輸出

上述程式碼將產生以下輸出:

The old string is This is a demo string!
The new String is This is a sample string!

在本教程中,我們學習了用新字串替換字串特定部分的不同方法。顯然,替換字串的最佳方法是使用replace()方法,它也採用正則表示式作為引數。儘管如此,我們仍然應該瞭解替換子字串的樸素方法的基本實現。

更新於:2023年1月3日

1000+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.