没有现成的函数,网上找的的实现也不好,自己写了一个
static string replace(string input, string pattern, string replacement, int n)
{
int count = 0;
string result = Regex.Replace(input, pattern, new MatchEvaluator((match) => {
result = count == n ? replacement : match.Value;
count++;
return result;
}));
return result;
}
调用方法
string sss = replace("xxx?xxx?dddd?", @"\?", "#", 1);
返回结果
xxx?xxx#dddd?