配列へのキャスト文字列 - CSVファイルからデータを読みます

コードの表示
1つの         パブリック 静的 文字列 [] casttoarray(文字列strLine変数の値)
 2          {
 3              一覧< ストリング > parsedData = 新しいリスト< ストリング > ();
4              ブール tokenInQuotes = 5              BOOL tokenContinued = 6               temp_println = "" ;
7              プリントライン= NULL ;
8  
9              もし(strLine.Trim()!= nullの10             {
 11                  // サイクル各キャラクタ
12                  のforeachチャー文字strLine変数の値)
 13                  {
 14                      であれば(tokenContinued == 15                      {
 16                          temp_println = プリントライン。
17                          プリントライン= temp_println。
18                      }
 19個の                     // カンマで分割トークン
20                      であれば(文字== ' ' 21                     {
 22                          もし(tokenInQuotes == 23                          {
 24                              // ストリップ引用開始
25を                             場合(!文字列 .IsNullOrEmpty(プリントライン)&& printLine.StartsWith(" \" " ))
 26                                  プリントライン= printLine.Substring(1、printLine.Length - 1 );
 27 28 // 引用終了ストリップ29を場合(!の文字列 .IsNullOrEmpty(プリントライン)&& printLine.EndsWith(" \" " 
                             
                             ))
 30                                  プリントライン= printLine.Substring(0、printLine.Length - 1 )。
31  
32                              // 単一引用符を有する任意の二重引用符置き換える
33を                             場合(!ストリング.IsNullOrEmpty(プリントライン))
 34                                  プリントライン= printLine.Replace(" \" \ " " " \" " );
 35  
36                              parsedData.Add(プリントライン) ;
 37                              プリントライン= NULL ;
 38                             = tokenContinued ;
39                              temp_println = NULL ;
40                          }
 41                          そう であれば(tokenInQuotes == 42                          {
 43                              プリントライン+ = キャラクタ;
44                              tokenContinued = 45                          }
 46                          続けます47                      }
 48  
49                      場合(文字== "\ " ' 50                      {
 51                          // 引用の開始を確認する
52                          場合(文字== ' \" " && tokenInQuotes == 53                          {
 54                              tokenInQuotes = ;
55                              プリントライン+ = 文字。
56                              tokenContinued = 57                              続け;
58                          }
 59  
60                          //引用の終了を確認する
61                          他の 場合(tokenInQuotes ==  &&文字== ' \」' 62                          {
 63                              tokenInQuotes = ;
 64                              プリントライン+ = 文字;
 65                              tokenContinued = ;
 66が                             継続;
 67                          }
 68                      }
 69  
70                      / / ハンドル他のすべての文字
71                      であれば(!文字= ' \」' &&文字=!' ' 72                      {
 73                          プリントライン+ = キャラクタ;
 74                          続ける;
 75                      }
 76                  }
 77                  であれば(tokenContinued == 78                  {
 79                      // 引用開始ストリップ
80の                     場合( !文字列 .IsNullOrEmpty(プリントライン)&& printLine.StartsWith(" \" " ))
 81                         プリントライン= printLine.Substring(1、printLine.Length - 1 )。
82  
83                      // 引用終了ストリップ
84を                     場合(!文字列 .IsNullOrEmpty(プリントライン)&& printLine.EndsWith(" \" " ))
 85                          プリントライン= printLine.Substring(0、printLine.Length - 1 );
 86 87 //は、いずれかを交換します単一引用符と二重引用符88 であれば(!文字列.IsNullOrEmpty(プリントライン))
 89 
                     
                                              プリントライン= printLine.Replace(" \" \ " " " \" " );
 90  
91                      parsedData.Add(プリントライン)
 92                      プリントライン= NULL ;
 93                      temp_println = NULL ;
 94                  }
 95              }
 96              リターンparsedData.ToArray()。
 97          }

 

ます。https://www.cnblogs.com/Jenny90/archive/2013/03/19/2969267.htmlで再現

おすすめ

転載: blog.csdn.net/weixin_34075268/article/details/93566817
おすすめ