C# 程式從字串中替換特殊字元


假設我們的字串是 -

string str = "abcd$ef$gh";

使用 Replace() 方法替換特殊字元。

string res = str.Replace('$', 'k');

下面的完整程式碼用於從字串中替換字元 -

例項

 即時演示

using System;
public class Program {
   public static void Main() {
      string str = "abcd$ef$gh";
      Console.WriteLine("Initial string = " + str);
      string res = str.Replace('$', 'k');
      // after replacing
      Console.WriteLine("Replaced string = " + res.ToString());
   }
}

輸出

Initial string = abcd$ef$gh
Replaced string = abcdkefkgh

更新日期:2020 年 6 月 22 日

5K+ 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

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