1 public static string[] casttoarray(string strLine)
2 {
3 List<string> parsedData = new List<string>();
4 bool tokenInQuotes = false;
5 bool tokenContinued = true;
6 string temp_println = "";
7 string printLine = null;
8
9 if (strLine.Trim() != null)
10 {
11 // Cycle Each Character
12 foreach (char character in strLine)
13 {
14 if (tokenContinued == true)
15 {
16 temp_println = printLine;
17 printLine = temp_println;
18 }
19 // Split Tokens At The Commas
20 if (character == ',')
21 {
22 if (tokenInQuotes == false)
23 {
24 //strip starting quotation
25 if (!string.IsNullOrEmpty(printLine) && printLine.StartsWith("\""))
26 printLine = printLine.Substring(1, printLine.Length - 1);
27
28 //strip ending quotation
29 if (!string.IsNullOrEmpty(printLine) && printLine.EndsWith("\""))
30 printLine = printLine.Substring(0, printLine.Length - 1);
31
32 //replace any double quotations with single quotation
33 if (!string.IsNullOrEmpty(printLine))
34 printLine = printLine.Replace("\"\"", "\"");
35
36 parsedData.Add(printLine);
37 printLine = null;
38 tokenContinued = false;
39 temp_println = null;
40 }
41 else if (tokenInQuotes == true)
42 {
43 printLine += character;
44 tokenContinued = true;
45 }
46 continue;
47 }
48
49 if (character == '\"')
50 {
51 // Check For Start Of Quotation
52 if (character == '\"' && tokenInQuotes == false)
53 {
54 tokenInQuotes = true;
55 printLine += character;
56 tokenContinued = true;
57 continue;
58 }
59
60 // Check for end of Quotations
61 else if (tokenInQuotes == true && character == '\"')
62 {
63 tokenInQuotes = false;
64 printLine += character;
65 tokenContinued = false;
66 continue;
67 }
68 }
69
70 // Handle all other characters
71 if (character != '\"' && character != ',')
72 {
73 printLine += character;
74 continue;
75 }
76 }
77 if (tokenContinued == false)
78 {
79 //strip starting quotation
80 if (!string.IsNullOrEmpty(printLine) && printLine.StartsWith("\""))
81 printLine = printLine.Substring(1, printLine.Length - 1);
82
83 //strip ending quotation
84 if (!string.IsNullOrEmpty(printLine) && printLine.EndsWith("\""))
85 printLine = printLine.Substring(0, printLine.Length - 1);
86
87 //replace any double quotations with single quotation
88 if (!string.IsNullOrEmpty(printLine))
89 printLine = printLine.Replace("\"\"", "\"");
90
91 parsedData.Add(printLine);
92 printLine = null;
93 temp_println = null;
94 }
95 }
96 return parsedData.ToArray();
97 }