如何在 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!
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP