C# string 保留数字英文字母

时间:2021-08-26 19:29:22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web; namespace Test
{
public class StringReplace
{
//只保留字符串数字
public static string GetNumberAlpha(string source)
{
string pattern = "[A-Za-z0-9]";
string strRet = "";
MatchCollection results = Regex.Matches(source, pattern);
foreach (var v in results)
{
strRet += v.ToString();
}
return strRet;
}
}
}
Console.WriteLine(StringReplace.GetNumberAlpha("8980afafadfa" +
" \r\n fff"));

测试代码,输出

C# string 保留数字英文字母