C# 正则表达式批量替换:将 'scqy' 替换为 'Scqy'
你可以使用正则表达式来进行批量替换。以下是一个示例代码,使用 'Regex.Replace' 方法来替换匹配到的字符串:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main(string[] args)
{
string input = 'public static void scqy()';
string pattern = @'\bpublic static void scqy\b';
string result = Regex.Replace(input, pattern, ReplaceMatch);
Console.WriteLine(result);
}
public static string ReplaceMatch(Match match)
{
string matchValue = match.Value;
string replacedValue = matchValue.Replace('scqy', 'Scqy');
return replacedValue;
}
}
在上面的代码中,我们定义了一个正则表达式模式 '\bpublic static void scqy\b' 来匹配 'public static void scqy' 这个字符串。然后,我们使用 'Regex.Replace' 方法将匹配到的字符串替换为 'Scqy'。
你可以根据自己的需求修改上面的代码来批量替换你的代码中的字符串。
原文地址: https://www.cveoy.top/t/topic/pcPz 著作权归作者所有。请勿转载和采集!