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.