使用 C# 程序将句子中的字符替换为星号


使用 Replace() 方法将一个字符替换为星号。

假设我们的字符串为 -

string str = "dem* text";

要进行替换,请使用 Replace() 方法 -

str.Replace('*', 'o');

以下是完整代码 -

示例

 在线演示

using System;
public class Program {
   public static void Main() {
      string str = "dem* text";
      Console.WriteLine("Initial string = " + str);
      string res = str.Replace('*', 'o');
      // after replacing
      Console.WriteLine("After replacing asterisk = " + res.ToString());
   }
}

输出

Initial string = dem* text
After replacing asterisk = demo text

更新于:22-6 月-2020

1 千+ 浏览量

开启您的职业生涯

通过完成课程获得认证

立即开始
广告
© . All rights reserved.