Skip to content

Split Comma

June 25, 2013
        public void StringTest()
            const string input = @"""Peter"",""Pan"",,""Hello, is it ok"",";
            var strings = Split(input);
            Assert.That(strings[0], Is.EqualTo("Peter"));
            Assert.That(strings[1], Is.EqualTo("Pan"));
            Assert.That(strings[2], Is.EqualTo(""));
            Assert.That(strings[3], Is.EqualTo("Hello, is it ok"));

        protected virtual string[] Split(string value)
            var regex = new Regex("(?:^|,)(\\\"(?:[^\\\"]+|\\\"\\\")*\\\"|[^,]*)");
            var collection = regex.Matches(value);
            var items = new string[collection.Count];
            var i = 0;
            foreach (Match match in collection)
                items[i++] = match.Groups[0].Value.Trim('"').Trim(',').Trim('"').Trim();
            return items;


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: