你可以使用正则表达式来实现这个功能。以下是一个在C#中使用正则表达式替换字符串的示例代码:\n\ncsharp\nusing System;\nusing System.Text.RegularExpressions;\n\nclass Program\n{\n static void Main()\n {\n string input = "a1b2c3d4e5f6g7h8i9j0";\n string pattern = "[^0-9,]";\n string replacement = "";\n\n string result = Regex.Replace(input, pattern, replacement);\n Console.WriteLine(result); // 输出:1,2,3,4,5,6,7,8,9,0\n }\n}\n\n\n在上面的示例中,我们使用[^0-9,]作为正则表达式的模式,它会匹配任何不是数字和逗号的字符。然后,我们将匹配到的字符替换为空字符串,从而只保留数字和逗号。最后,我们使用Regex.Replace方法将模式应用于输入字符串,并打印替换结果。

C# 字符串替换:只保留数字和逗号

原文地址: https://www.cveoy.top/t/topic/qdyi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录