using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Reflection; using System.Text.RegularExpressions; namespace ConsoleApplication1 { public static class Program { /// <summary> /// 入口函数 /// </summary> /// <param name="args"></param> static void Main( string[] args) { List< string> oldStrList = new List< string>() { " 12 ", " ZH ", " CN ", " 2E ", " 0A ", " 56 ", " FF ", " 00 " }; Console.Write( " Old List: " + oldStrList.Count + " -> "); foreach ( string str in oldStrList) Console.Write(str + " "); Console.WriteLine( ""); List< string> newHexList = oldStrList.HexFilter( (p) => { bool blStatus = false; try { Convert.ToInt32(p, 16); blStatus = true; } catch { blStatus = false; } return blStatus; } ).ToList(); Console.Write( " New List: " + newHexList.Count + " -> "); foreach ( string str in newHexList) Console.Write(str + " "); Console.ReadLine(); } /// <summary> /// 过滤掉非十六进制字符 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source"></param> /// <param name="predicate"></param> /// <returns></returns> static IEnumerable<T> HexFilter<T>( this IEnumerable<T> source, Func<T, bool> predicate) { foreach (T item in source) { if (predicate(item)) yield return item; } } } }