'How to Split a string using the delimiter with quotes and white spaces?

I want to split a string using only the delimiter. But my code is breaking if the string has white spaces.

1233;"Order";Placed at 10 AM;1

When I split it, the out put is

1233
\"Order"\
Placed 
at
10
AM
1

Expected output is:

1233
\"Order"\
Placed at 10 AM
1

My Code:

                    var result = columns[0].Split(';')
                 .Select((element, index) => index % 2 == 0  
                  ? element.Split(new[] { ' ' }, StringSplitOptions.None)                                             : new string[] { element })  // Keep the entire item
                 .SelectMany(element => element).ToList();

Thanks for fixing this issue.



Solution 1:[1]

Just keep only the first line.

var result = columns[0].Split(';');

Solution 2:[2]

You are explicitely also splitting on spaces: Split(new[] { ' ' }.

If you only want to split on semicolons, this is sufficient:

var result = columns[0].Split(';');

You might also consider using a library like CsvHelper.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2147873
Solution 2 Klaus Gütter