魅影

不是强者,以后会是!

导航

C# 字符串操作

从字符串:aba,3456df3!344dfdf    提取 34563344

暂时就想到这几种方法,以后有了再补充

引用:using System.Diagnostics;

 

Stopwatch sp = new Stopwatch();
            sp.Start();

            string str= "aba,3456df3!344dfdf";

            string result = string.Empty;


            //测试结果 34563344   用时 00:00:00.0000094
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsNumber(str[i]))
                {
                    result += str[i];
                }
            }

            ////测试结果 34563344 运行时间 00:00:00.0000081
            //foreach (char strString in str.ToCharArray(0, str.Length - 1))
            //{
            //    if (char.IsNumber(strString))
            //    {
            //        result += strString;
            //    }
            //}

            ////测试结果 34563344 运行时间 00:00:00.0000030
            //result = str.Substring(4, 4) + str.Substring(10, 1) + str.Substring(12, 3);

            // 测试结果 34563344 运行时间 00:00:00.0000726
            //result = Regex.Replace(str, @"[^\d+]", "");

            sp.Stop();
           
             HttpContext.Current.Response.Write(result +"<br/>"+ sp.Elapsed);

posted on 2011-05-01 02:07  李豫川  阅读(162)  评论(0编辑  收藏  举报