C#开发之——字符串替换(4.4)

一 概述

  • 字符串的替换操作是指将字符串中指定的字符串替换成新字符串
  • 在C#中替换字符串的方法是Replace方法

二 实例 在 Main 方法中从控制台输入一个字符串,然后将字符串中所有的‘,’替换成‘_’

2.1 代码

复制
1
2
3
4
5
6
7
8
9
10
11
12
class Program
   {
       static void Main(string[] args)
       {
           string str = Console.ReadLine();
           if (str.IndexOf(",") != -1)
           {
               str = str.Replace(",","_");
           }
           Console.WriteLine("替换后的字符串为:"+str);
       }
   }

2.2 执行结果

猜你喜欢

转载自blog.csdn.net/Calvin_zhou/article/details/107408484
今日推荐