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" + " fff"));
测试代码,输出