0

I'm developing a C# based application and want to use "," (Comma) delimiter to split strings. The problem arrives when the comma is part of my string and not a delimiter (like we have in addresses for example). Example:

Input: "A,B,Venus, New York 10001"

Actual output: "A", "B", "Venus", "New York"

Required output: "A", "B", "Venus, New York"

What is the best practice for this kind of issue? Thanks.

user1630359
  • 103
  • 8
  • 4
    Do not reinvent the wheel. Use an existing CSV parser. You should change your input to something like `A,B,"Venus, New York 10001"` – Hamid Pourjam May 01 '17 at 08:16
  • Or even better, don't use comma delimited text in the first place. If you can control your input, use JSON or even XML. – Zohar Peled May 01 '17 at 08:27
  • Will do it, Thanks. – user1630359 May 01 '17 at 08:31
  • Is the field with the comma the last one on the line? – Steve May 01 '17 at 08:34
  • Not sure if the answers in the duplicate question could really solve the poster requirements here. Irregular CSVs are very problematic if you don't have any control on the tool that produces them – Steve May 01 '17 at 09:36

1 Answers1

1

I recommend you to take a look at dotctor and Zohar Peled comments. This could result in a more robust solution. However, if you still need to split the string as mentioned in your question you could do the following.

I notice that the input is split on a comma that is not followed by a whitespace. When there is a whitespace behind the comma the string should not be split. If this is always the case for your, then the following will work:

static void Main(string[] args)
{
    var result = SplitOnCommaNotFollowedByWhiteSpace("A,B,Venus, New York 10001");
    //Result: string["A", "B", "Venus, New York"]
}

public static string[] SplitOnCommaNotFollowedByWhiteSpace(string input)
{
    var regex = new Regex(@",(?!\s)", RegexOptions.IgnoreCase);
    return regex.Split(input);
}
Community
  • 1
  • 1
Jurjen
  • 1,376
  • 12
  • 19