FilterNoises 全角转半角 过滤所有"标点符号",空格


//csc.exe /r:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\microsoft.visualbasic.dll noname1.cs
namespace ConsoleApplication
{
    using System;
    using System.Text;
    using JeffreyZhao;
    using Microshaoft;
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class Class1
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        //[STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: 在此处添加代码以启动应用程序
            //
            int iteration = 10000;// * 1000;
            string noises = "`~!@#$%&*()_+-=\t{}|[]\\:\";'<>?,./\0\r\n ";
            noises += "~!#¥%……&×()——+·-={}|【】\:“”;‘’'<>?,./ ";
            string s = "B按ah`~!@#$%&*()_+-=\t{}|撒旦撒大[]\\:\";'<>?,./\0\r\n ";
            s += "~!#¥%……&×()——+·-={}|【】\:“”;‘’'<>?,./ ";
            //int l = noises
            s += "AB按时打算阿斯顿llAA啊ah";
            //s = "asdsad阿斯顿撒ooo";
            CodeTimer.Time
                        (
                            "1",
                            iteration,
                            () =>
                            {
                                StringHelper.FilterNoises(s, noises);
                            }
                        );
            CodeTimer.Time
                        (
                            "2",
                            iteration,
                            () =>
                            {
                                StringHelper.FilterNoises(s, noises);
                            }
                        );
            CodeTimer.Time
                        (
                            "3",
                            iteration,
                            () =>
                            {
                                StringHelper.FilterNoises(s);
                            }
                        );
            Console.WriteLine("Hello World");
            Console.WriteLine(Environment.Version.ToString());
        }
    }
}
namespace JeffreyZhao
{
    using System;
    using System.Diagnostics;
    using System.Threading;
    using System.Runtime.InteropServices;
    public static class CodeTimer
    {
        public static void Initialize()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Time("", 1, () => { });
        }
        public static void Time(string name, int iteration, Action action)
        {
            if (String.IsNullOrEmpty(name))
            {
                return;
            }
            // 1.
            ConsoleColor currentForeColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(name);
            // 2.
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            int[] gcCounts = new int[GC.MaxGeneration + 1];
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                gcCounts[i] = GC.CollectionCount(i);
            }
            // 3.
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ulong cycleCount = GetCycleCount();
            for (int i = 0; i < iteration; i++)
            {
                action();
            }
            ulong cpuCycles = GetCycleCount() - cycleCount;
            watch.Stop();
            // 4.
            Console.ForegroundColor = currentForeColor;
            Console.WriteLine("\tTime Elapsed:\t{0:N0}{1}", watch.ElapsedMilliseconds, "ms");
            Console.WriteLine("\tCPU Cycles:\t{0:N0}", cpuCycles);
            // 5.
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                int count = GC.CollectionCount(i) - gcCounts[i];
                Console.WriteLine("\tGen {0}:\t\t{1}", i, count);
            }
            Console.WriteLine();
        }
        private static ulong GetCycleCount()
        {
            ulong cycleCount = 0;
            //Windows Vista
            //QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
            return cycleCount;
        }
        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);
        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();
    }
}
//csc.exe /r:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\microsoft.visualbasic.dll noname1.cs
namespace Microshaoft
{
    using System;
    using Microsoft.VisualBasic;
    using System.Globalization;
    public class StringHelper
    {
        public static string FilterNoises(string text, string noises)
        {
            text = Strings.StrConv(text, VbStrConv.Narrow, 0);
            //Console.WriteLine("[{0}]", text);
            //char[] a = noises.ToCharArray();
            int l = noises.Length;
            for (int i = 0; i < noises.Length; i++)
            {
                string r = noises.Substring(i, 1);
                //text = text.Replace(r, "");
                //text= Strings.Replace(text, r, "");
                //text = Strings.Replace(text, "o", "i");
            }
            ///            foreach (char c in a)
            ///            {
            ///                string s = new string(new char[] {c});
            ///                text = text.Replace(s, "");
            ///            }
            return text;
        }
        public static string FilterNoises2(string text, string noises)
        {
            text = Strings.StrConv(text, VbStrConv.Narrow, 0);
            //Console.WriteLine("[{0}]", text);
            char[] a = noises.ToCharArray();
            int l = noises.Length;
            ///            for (int i = 0; i < noises.Length; i++)
            ///            {
            ///                string r = noises.Substring(i, 1);
            ///                text = text.Replace(r, "");
            ///            }
            foreach (char c in a)
            {
                string s = new string(new char[] { c });
                text = text.Replace(s, "");
            }
            return text;
        }
        public static string FilterNoises(string text)
        {
            text = Strings.StrConv(text, VbStrConv.Narrow, 0);
            int l;
            l = text.Length;
            int i = 0;
            if (l > 0)
            {
                do
                {
                    UnicodeCategory uc = Char.GetUnicodeCategory(text, i);
                    string ucName = Enum.GetName(uc.GetType(), uc).ToLower();
                    if (!ucName.Contains("letter"))
                    {
                        l = text.Length;
                        string r = text.Substring(i, 1);
                        text = text.Replace(r, "");
                        if (l == text.Length)
                        {
                            break;
                        }
                    }
                    else
                    {
                        i++;
                    }
                    if (i >= text.Length)
                    {
                        break;
                    }

                } while (l >= text.Length);
            }
            return text;
        }
    }
}

原文地址:https://www.cnblogs.com/Microshaoft/p/1564840.html