我不知道如何分割这个字符串:
字符串为'Allocation: Randomized, Endpoint Classification: Safety Study, Intervention Model: Single Group Assignment, Masking: Double Blind (Subject,
Caregiver, Investigator, Outcomes Assessor), Primary Purpose: Treatment'
目前使用的分割语法:
string.Split(',');
结果:
[0]: Allocation: Randomized
[1]: Endpoint Classification: Safety Study
[2]: Intervention Model: Single Group Assignment
[3]: Masking: Double Blind (Subject,
[4]: Caregiver,
[5]: Investigator,
[6]: Outcomes Assessor)
[7]: Primary Purpose: Treatment
但我想要的结果是:
[0]: Allocation: Randomized
[1]: Endpoint Classification: Safety Study
[2]: Intervention Model: Single Group Assignment
[3]: Masking: Double Blind (Subject, Caregiver, Investigator, Outcomes Assessor)
[4]: Primary Purpose: Treatment
有人可以帮我纠正我的字符串拆分语法吗?
答案 0 :(得分:4)
在这种情况下我会使用RegEx
string input = "Allocation: Randomized, Endpoint Classification: Safety Study, Intervention Model: Single Group Assignment, Masking: Double Blind (Subject, Caregiver, Investigator, Outcomes Assessor), Primary Purpose: Treatment";
string[] result = System.Text.RegularExpressions.Regex.Split(input, @",(?![^(]*\))");
注意:不适用于嵌套括号
答案 1 :(得分:1)
而不是拆分(','),尝试使用拆分(':'),然后运行您的阵列并将每两个成员添加到一起。之后,您需要在每个字符串上使用TrimEnd(',')或TrimStart(','),以便按照您要求的方式对其进行格式化。
可能是这样的一起加入:
for each (int i in Array)
{
[i] = [i]+[i+1];
i++;
}
答案 2 :(得分:0)
您可以先在(
和)
进行拆分,这样您就可以获得可以进一步拆分的部分((
之前)和不应拆分的部分({{1之后)在(
之前。)
然后,您将按)
拆分所有创建的块,但是如您所知,每个“括号”块,您可以根据需要重新连接小块。
很抱歉没有提供样品,在手机上打字太多了。