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.