1

I have written an exporter that exports strings to .csv

Thing is, some of these strings have commas in already:

2 CAR MODELS, 5 TEAMS,2 CAR MODELS, 5 TEAMS

You see that the commas added in the strings by hand have one white space after them. The one I added has no whitespace.

I have written code to read the string and split it:

while ((line = stringReader.ReadLine()) != null)
        {
            // split the string at each comma
            string[] split = line.Split(new char[] { ',' });

What i need is for the string to split at ',' and NOT at ', '.

Any ideas?

I cant use any third party code like this:

http://www.codeproject.com/KB/database/CsvReader.aspx

5
  • 1
    normally you would delimit all your csv fields in some style of quotes. I.e. "2 CAR MODELS", "5 TEAMS","2 CAR MODELS","5 TEAMS". Then you would only need to split on the commas Commented Dec 1, 2011 at 16:04
  • Why can't you use the CodeProject CSV-Reader? It's open-source. Commented Dec 1, 2011 at 16:05
  • @Tim -- Project Limitation. Its not my call. If I could use it, I would. :( Commented Dec 1, 2011 at 16:07
  • What is the limitation exactly, no 3rd parties, no incompatible licenses, what exactly is the licenses and why? Commented Dec 1, 2011 at 16:08
  • We are not allowed to -- "include the text of the license in a file called "LICENSE", and make reference to it in a file called "README"" as stated in the MIT licence of that 3rd party code. Its not my call, but my lead programmer doesnt want it. Same with Boost and such, we cant use that. Commented Dec 1, 2011 at 16:17

3 Answers 3

3

You can use the TextFieldParser - it is in the Microsoft.VisualBasic.FileIO namespace, so not "thirdparty".

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

Comments

1

You might consider using Regex.Split(String arg1, String arg2). It should allow you to split a string according to a regular expression.

You can then use a regex like this:

,[^\s]+

This should allow you to split values which have a comma but are not followed with one or more spaces.

1 Comment

-1 That will not split on the comma, that will split on the comma and the following non-space characters.
1

You can do this with Regex.Split in the System.Text.RegularExpressions namespace.

http://msdn.microsoft.com/en-us/library/b5k3ak3e.aspx

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.