"景先生毕设|www.jxszl.com

C# replace只替换一次或者多次的方法

2022-12-20 14:52编辑: www.jxszl.com景先生毕设

C# replace只替换一次或者多次的方法


最优办法:正则表达式(Regex)替换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "免费论文网|www.jxszl.com|免费论文网";
            Regex regex = new Regex("免费");//要替换字符串"免费"
            str = regex.Replace(str, "51", 1);// 要替换字符串"免费" 替换成“51”   ,1 是  最后一个参数是替换的次数
            Console.WriteLine(str);//输出:51论文网|www.jxszl.com|免费论文网
            Console.ReadKey();
        }
    }
}

原文链接:http://www.jxszl.com/biancheng/C/84434.html