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 日

5 千 + 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.