2017-07-16 15 views
0

カンマで文字列を分割し、内部で分割しない方法 - ""はC#のみです。例えばカンマで区切らずにカンマで区切られた文字列 "#"を使用して "#"のみ

この文字列"aa","b,b","asdds","sd,fd,sd,,f"

この配列/リストへ

- 分割するaab,basddssd,fd,sd,,f

+0

使用正規表現:https://www.dotnetperls.com/ regex-splitを実行し、正規表現の値をループしてリストに追加します。 – Chrotenise

+0

CSVパーサーを検索します。良い人なら誰でもこの事件を処理します。 – juharr

+0

'' [\ w、] + "'が実行します。 –

答えて

0
string s = "\"aa\",\"b,b\",\"asdds\",\"sd,fd,sd,,f\""; // The string (\" = ") when write it inside string 
List<string> s1 = new List<string>(); // List of the strings without the , and the " 
string s2 = ""; // string for adding into the list 
bool 
    first = false, // If arrive to the first " 
    second = false; // If arrive to the second " 
foreach (char c in s) // Move over every char in the string 
{ 
    if (second) // If reach the second 
    { 
     s1.Add(s2); // Add the string to the list 
     s2 = ""; // Make s2 ready for new string 
     first = false; // Make first be ready for another string 
     second = false; // Make second be ready for another string 
    } 
    if (first) // If the string in the quotemark started 
    { 
     if (c == '"') // If the string in the quotemark ended 
     { 
      second = true; // Reach the second quotemark 
     } 
     else // If the string didn't end 
     { 
      s2 += c; // Add the char to the string 
     } 
    } 
    if (c == '"' && !first && !second) // If the string just reach the first quotemark in a string 
    { 
     first = true; // Reach the first quotemark 
    } 
} 
if (second&&first) //For the last string that missed at the end 
{ 
    s1.Add(s2); // Add the string to the list 
} 
0
string sample = "aa","b,b","asdds","sd,fd,sd,,f"; 
sample = sample.Replace("\",\", "&"); 
string[] targetA = sample.Split('&'); 
関連する問題