Reputation: 39
I don't know how to split this string:
The string is 'Allocation: Randomized, Endpoint Classification: Safety Study, Intervention Model: Single Group Assignment, Masking: Double Blind (Subject,
Caregiver, Investigator, Outcomes Assessor), Primary Purpose: Treatment'
Currently used split syntax:
string.Split(',');
results in:
[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
but the result I would like is:
[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
Could someone help me correct my string split syntax?
Upvotes: 1
Views: 237
Reputation: 3509
You could split at (
and )
first, so you will get parts that can be split further (before (
) and parts that should not be split (after (
, before)
).
You will then split all created blocks by ,
, but as you know about each of the 'brackets' block, you can reconnect the small ones as needed.
Sorry for not providing a sample, is too much work typing on a mobile phone.
Upvotes: 0
Reputation: 45967
I would use RegEx
in this case
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, @",(?![^(]*\))");
Note: does not work for nested brackets
Upvotes: 4
Reputation: 166
instead of Split(','), try using Split(':') and then run through your array and add every two members together. afterwards, youll need to use TrimEnd(',') or TrimStart(',') on each string to get it formatted exactly the way you have asked.
maybe something like this for the adding together:
for each (int i in Array)
{
[i] = [i]+[i+1];
i++;
}
Upvotes: 1