C# string 保留数字英文字母

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"));

测试代码,输出

 


posted @ 2018-06-21 11:03  zhaogaojian  阅读(4781)  评论(0编辑  收藏  举报