1

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?

1
  • does any of the given answers work for you? Commented Oct 6, 2016 at 7:24

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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++;
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.