如何在 C# 中查詢替換字串中的單詞?


首先,設定要替換的字串。

string str = "Demo text!";

現在使用 replace() 方法替換上述字串。

string res = str.Replace("Demo ", "New ");

以下是替換字串中單詞的完整程式碼。

 即時示例

using System;
public class Demo {
   public static void Main() {

      string str = "Demo text!";
      Console.WriteLine(str);

      string res = str.Replace("Demo ", "New ");
      Console.WriteLine("After replacing...");
      Console.WriteLine(res);
   }
}

輸出

Demo text!
After replacing...
New text!

更新日期:2020 年 6 月 22 日

4K+ 檢視次數

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.