// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; using System.Collections.Generic; namespace E { public class Program { public static string[] Find1(string[] lst, string txt) { return Array.FindAll(lst, (string s) => txt.Contains(s)); } public static string[] Find2(string[] lst, string txt) { HashSet hs = new HashSet(txt.Split(' ', ',', '.')); return Array.FindAll(lst, (string s) => hs.Contains(s)); } private static void TestSmall() { string[] total = { "IBM", "HP", "Microsoft", "Oracle", "Intel", "Cisco", "SAP", "Dell", "AMD", "Redhat" }; string txt = "SUN, IBM, Oracle og Redhat har hver deres app server"; string[] found1 = Find1(total, txt); foreach(string s in found1) { Console.WriteLine(s); } string[] found2 = Find2(total, txt); foreach(string s in found2) { Console.WriteLine(s); } } private static Random rng = new Random(); private const string VALID = "ABCDEFGHIJKLMOPQRSTUWVXYZ"; private static string GenWord() { int len = rng.Next(1, 10); StringBuilder sb = new StringBuilder(); for(int i = 0; i < len; i++) { sb.Append(VALID[rng.Next(VALID.Length)]); } return sb.ToString(); } private static void TestBig(int nwlst, int nwtxt) { string[] total = new String[nwlst]; for(int i = 0; i < nwlst; i++) total[i] = GenWord(); StringBuilder sb = new StringBuilder(); for(int i = 0; i < nwtxt; i++) sb.Append(GenWord()); string txt = sb.ToString(); DateTime dt1 = DateTime.Now; string[] found1 = Find1(total, txt); DateTime dt2 = DateTime.Now; Console.WriteLine((dt2 - dt1).TotalSeconds); DateTime dt3 = DateTime.Now; string[] found2 = Find2(total, txt); DateTime dt4 = DateTime.Now; Console.WriteLine((dt4 - dt3).TotalSeconds); } public static void Main(string[] args) { TestSmall(); TestBig(5000, 5000); } } }