0
如何用逗号分割字符串并且不在内部分割 - 仅用于c#的""
。用逗号分割字符串,不用在内部分割“”,仅用c#“
例如此字符串"aa","b,b","asdds","sd,fd,sd,,f"
向该阵列/列表 - aa
,b,b
,asdds
,sd,fd,sd,,f
如何用逗号分割字符串并且不在内部分割 - 仅用于c#的""
。用逗号分割字符串,不用在内部分割“”,仅用c#“
例如此字符串"aa","b,b","asdds","sd,fd,sd,,f"
向该阵列/列表 - aa
,b,b
,asdds
,sd,fd,sd,,f
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
}
string sample = "aa","b,b","asdds","sd,fd,sd,,f";
sample = sample.Replace("\",\", "&");
string[] targetA = sample.Split('&');
使用正则表达式来拆分:https://www.dotnetperls.com/ regex-split,然后遍历正则表达式的值并将其添加到列表 – Chrotenise
查找CSV解析器。任何好的人都会处理这种情况。 – juharr
'“[\ w,] +”'会做。 –