我有像
这样的字符串"11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1"
我想用char'拆分它,'但我不想在'()'中包含','。请帮助如何在c#中拆分这样的字符串。 忽略char','in()。
输出应该是:
array of string = [
"11",
"Standard(db=S,api=Standard)",
"UI",
"1(db=1,api=STANDARD)",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36",
"1010"
等
答案 0 :(得分:3)
如果模式始终,您可以使用RegularExpression分割分隔符(逗号)而不是匹配标记。识别分隔符的逗号需要相对简单:
",(?=[^\)]*(?:\(|$))"
答案 1 :(得分:2)
这是一个解析器
public static IEnumerable<string> Parse(string Input)
{
int depth = 0;
StringBuilder Line = new StringBuilder();
foreach (char item in Input)
{
if (depth == 0 && item == ',')
{
yield return Line.ToString();
Line = new StringBuilder();
}
else
{
Line.Append(item);
if (item == '(')
{
depth++;
}
if (item == ')')
{
depth--;
}
}
}
if (Line.Length > 0)
yield return Line.ToString();
}
用途:
var result = Parse(input);
答案 2 :(得分:1)
这是一个简单直接的解析器。不是防弹,根据您的需要,可能需要进行一些修改:
static void Main(string[] args)
{
var toSplit = "11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1";
var list = new List<string>();
var isInside = 0;
var currentPart = string.Empty;
for (int i = 0; i < toSplit.Length; i++)
{
var chr = toSplit[i];
switch (chr)
{
case ',':
if(isInside == 0)
{
list.Add(currentPart);
currentPart = string.Empty;
}
break;
case '(':
isInside++;
currentPart += chr;
break;
case ')':
isInside--;
currentPart += chr;
break;
default:
currentPart += chr;
break;
}
}
foreach (var part in list)
{
Console.WriteLine(part);
}
Console.ReadLine();
}
答案 3 :(得分:0)
这应该做你想要的。它会在delimitter
之外将您的输入分为任何括号
static IEnumerable<string> SplitStringMyWay(string text, char delimitter)
{
string[] internalItems = text.Split(delimitter);
List<string> theItems = new List<string>();
string newItem = string.Empty;
int openParenthesis = 0;
foreach (string item in internalItems)
{
if (openParenthesis != 0) newItem += ",";
newItem += item;
openParenthesis += GetCharCount('(', item);
openParenthesis -= GetCharCount(')', item);
if (openParenthesis == 0)
{
theItems.Add(newItem);
newItem = string.Empty;
}
}
return theItems;
}
static int GetCharCount(char value, string text)
{
int count = 0;
foreach (char character in text)
{
if (character == value)
{
count++;
}
}
return count;
}
代码不是testet,可能包含错误。如果您觉得有人可以自由编辑我的答案
答案 4 :(得分:0)
IMO最简单的方法是首先使用','
拆分字符串,然后将字符串与&#39;(&#39;和&#39;)&#39;连接起来。一起签字:
string value = @"11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1";
List<string> list = new List<string>();
string[] temp = value.Split(',');
for (var i = 0; i < value.Split(',').Length; i++) { if (temp[i].Contains('(')) continue; if (temp[i].Contains(')')) { list.Add(temp[i - 1] + temp[i]); continue; } list.Add(temp[i]); }
foreach (var item in list) Console.WriteLine(item);
输出:
11
Standard(db=S,api=Standard)
UI
1(db=1,api=STANDARD)
P.S。 当然你也可以写你的解析器:
static string[] MySplit(string dirty, char delimiter = ',', string ignoreInside = "()")
{
StringBuilder sb = new StringBuilder();
bool sectionStarted = false;
List<string> result = new List<string>();
for (int i = 0; i < dirty.Length; i++)
{
if (!sectionStarted && dirty[i] == delimiter)
{
result.Add(sb.ToString());
sb.Clear(); continue;
}
if (ignoreInside.Contains(dirty[i])) sectionStarted = dirty[i] == ignoreInside[0];
sb.Append(dirty[i]);
}
return result.ToArray();
}
用法:var result = MySplit(value);
输出与上面的示例相同。
答案 5 :(得分:0)
这是有效的
var values = "11,Standard(db=S,api=Standard),UI,1(db=1,api=STANDARD),Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36,1010,9999,1000,9998.0,,1";
var splitList = GetSplitList(values);
public static List<string> GetSplitList(string values)
{
var splitList = new List<string>();
var retValue = string.Empty;
foreach (var value in values.Split(','))
{
if (!string.IsNullOrEmpty(retValue) && !value.Contains(")"))
{
retValue += string.Format("{0},", value);
continue;
}
if (value.Contains("("))
{
retValue += string.Format("{0},", value);
continue;
}
if (value.Contains(")"))
{
retValue += value;
splitList.Add(retValue);
retValue = string.Empty;
continue;
}
splitList.Add(value);
}
return splitList;
}