for each, list si predicate
Va rog sa folositi acest URL NOU
de la
http://serviciipeweb.ro/iafblog/
Am avut ieri de selectat dintr-o lista de carti (aflate intr-un fisier txt) cele care ma intereseaza - mai bine spus, de scos cele care nu ma intereseaza - si trimis lista inapoi .
La inceput, am inceput sa fac normal:
30 string[] lines = File.ReadAllLines(@"C:\lista.txt");
31 foreach (string line in lines)
32 {
33 if (line.ToLower().IndexOf("java") > 0)
34 continue;
35
36 if (line.ToLower().IndexOf("autocad") > 0)
37 continue;
38
39 if (line.ToLower().IndexOf("cisco") > 0)
40 continue;
41
42 l.Add(line);
43 }
44 string strNewFile = @"C:\lista1.txt";
45 if (File.Exists(strNewFile))
46 File.Delete(strNewFile);
47
48 File.WriteAllLines(strNewFile, l.ToArray());
49
50 System.Diagnostics.Process.Start(strNewFile);
( de aici se vede clar ca am scos java, cisco , si altele ...)
Apoi - am inceput sa gindesc - si sa imi aduca aminte ca exista ceea ce se numeste Predicate
Asa ca am ajuns la urmatorul text :
28 static Predicate<string> NotInStringFirstTry(string x)
29 {
30
31 return new Predicate<string>(
32 delegate(string line)
33 {
34
35 if (x == null && line == null)
36 return false;
37 if (x == null)
38 return true;
39 if (line == null)
40 return true;
41 return (line.ToLower().IndexOf(x.ToLower()) < 0);
42 }
43 );
44 }
45 static void Main(string[] args)
46 {
47 string[] lines = File.ReadAllLines(@"C:\lista.txt");
48
49 List<string> l = new List<string>();
50 l.AddRange(lines);
51
52
53 l = l.FindAll(NotInStringFirstTry("java"));
54 l = l.FindAll(NotInStringFirstTry("cisco"));
55 l = l.FindAll(NotInString("autocad"));
56
57 string strNewFile = @"C:\lista1.txt";
58 if (File.Exists(strNewFile))
59 File.Delete(strNewFile);
60
61 File.WriteAllLines(strNewFile, l.ToArray());
62
63 System.Diagnostics.Process.Start(strNewFile);
64 }
Nu numai ca e mai compact - e si mai simplu de inteles ...
Iar la a 3-a gindire mi-am dat seama ca fac prea multe iteratii in array - asa ca m-ar ajuta o singura functie:
29 static Predicate<string> NotInString(params string[] x)
30 {
31
32 return new Predicate<string>(
33 delegate(string line)
34 {
35
36 if (x == null && line == null)
37 return false;
38 if (x == null)
39 return true;
40 if (x.Length == 0)
41 return true;
42 if (line == null)
43 return true;
44 foreach (string word in x)
45 {
46 bool b = (line.ToLower().IndexOf(word.ToLower()) > -1);
47 if (b)
48 return false;
49 }
50
51 return true;
52 }
53 );
54 }
55
56 static void Main(string[] args)
57 {
58 string[] lines = File.ReadAllLines(@"C:\lista.txt");
59
60 List<string> l = new List<string>();
61 l.AddRange(lines);
62 l = l.FindAll(NotInString("java", "cisco", "autocad"));
63
64 string strNewFile = @"C:\lista1.txt";
65 if (File.Exists(strNewFile))
66 File.Delete(strNewFile);
67
68 File.WriteAllLines(strNewFile, l.ToArray());
69
70 System.Diagnostics.Process.Start(strNewFile);
71 }
Plina de peripetii, programarea asta ... Daca aveti o alta sugestie, astept sa imi spuneti
( codul a fost copiat cu ajutorul CSAH)
http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/
Weblog post by 'admin' on 'for each, list si predicate'
Categories:.NET;programare
0 Comments:
Trimiteți un comentariu
<< Home