回答編集履歴

1

f___

2018/11/07 03:08

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  ,,,INIT,1,,,,,,,
26
26
 
27
- ,,,WATI_T,100,,,,,,,
27
+ ,,,WAIT_T,100,,,,,,,
28
28
 
29
29
  ,,,DISP,,,,,,,,
30
30
 
@@ -33,3 +33,61 @@
33
33
 
34
34
 
35
35
  ```
36
+
37
+
38
+
39
+ ```csharp
40
+
41
+ using System;
42
+
43
+ using System.Linq;
44
+
45
+ using System.Collections.Generic;
46
+
47
+ using System.Text;
48
+
49
+ using System.IO;
50
+
51
+
52
+
53
+ namespace CsvParser
54
+
55
+ {
56
+
57
+ class Program
58
+
59
+ {
60
+
61
+ static void Main(string[] args)
62
+
63
+ {
64
+
65
+ var txt = File.ReadLines(@"C:\work\Item.csv", Encoding.GetEncoding("shift_jis")).ToArray();
66
+
67
+ if (IsShutDownOff(txt)) { Console.WriteLine("中断(シャットダウンオフ)"); Console.ReadKey(); return; }
68
+
69
+ if (IsSerialNumberLength11(txt)) { Console.WriteLine("中断(S/N_LEN=11)"); Console.ReadKey(); return; }
70
+
71
+ var initVal = findNextOfKey(txt, "INIT");
72
+
73
+ var waitVal = findNextOfKey(txt, "WAIT_T");
74
+
75
+ Console.WriteLine($"INIT = {initVal} , WAIT_T = {waitVal}");
76
+
77
+ }
78
+
79
+ static bool IsShutDownOff(IEnumerable<string> lines) => lines.Any(line => line.Contains("Shutdown=OFF"));
80
+
81
+ static bool IsSerialNumberLength11(IEnumerable<string> lines) => lines.Any(line => line.Contains("S/N_LEN=11"));
82
+
83
+ static string firstLineWhichHas(IEnumerable<string> lines, string key) => lines.FirstOrDefault(line => line.Contains(key));
84
+
85
+ static string getNextOf(string line, string key) => line?.Split(',').SkipWhile(col => col.Trim().ToLower() != key.ToLower()).ElementAt(1).Trim() ?? string.Empty;
86
+
87
+ static string findNextOfKey(IEnumerable<string> lines, string key) => getNextOf(firstLineWhichHas(lines, key), key);
88
+
89
+ }
90
+
91
+ }
92
+
93
+ ```