String split and wrapper character in c#

  • Thread starter Thread starter thiago_bagua
  • Start date Start date
T

thiago_bagua

Hi all,

I have a csv file exported from excel. On cells where there was a comma
in the text, it wraps the character with double quotes. For example:

Column 1,"column two, and some more stuff",Column 3

In a c# dll, .NET framework 1.1, I'm reading the file using a
streamreader then retrieving each line into a string using
reader.ReadLine().

If I use the normal string.Split(',') it ends up splitting the line
into 4 strings:
Column 1
"column two
and some more stuff"
Column 3

Whereas I need it to be split into this:
Column 1
"column two, and some more stuff"
Column 3

Is there a proven and efficient method of splitting the line but
keeping the wrapper character in mind?

Cheers,
Thiago
 
Hmm, I had a look at that forum, pretty interesting stuff. It looks
like using the Microsoft Jet OleDb adapter would be a good way to go
about it. My only concern is: my DLL will be receiving the csv data in
a stream in the future instead of picking it up from a file... Does
this mean I'd have to save the stream to a file then use the OleDb
adapter to load it?

Thanks,
Thiago
Thiago,

I would actually use the classes in the System.Data.OleDb namespace for
this. Check out the following thread on MSDN forums, it should give you a
good start:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=6015&SiteID=1

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi all,

I have a csv file exported from excel. On cells where there was a comma
in the text, it wraps the character with double quotes. For example:

Column 1,"column two, and some more stuff",Column 3

In a c# dll, .NET framework 1.1, I'm reading the file using a
streamreader then retrieving each line into a string using
reader.ReadLine().

If I use the normal string.Split(',') it ends up splitting the line
into 4 strings:
Column 1
"column two
and some more stuff"
Column 3

Whereas I need it to be split into this:
Column 1
"column two, and some more stuff"
Column 3

Is there a proven and efficient method of splitting the line but
keeping the wrapper character in mind?

Cheers,
Thiago
 
Yes, it's a limitation of both the jet driver and the odbc driver that
the data must be in an action file.

Optionally, you can check out the commercial product that I sell,
http://www.csvreader.com .

Bruce Dunwiddie

Hmm, I had a look at that forum, pretty interesting stuff. It looks
like using the Microsoft Jet OleDb adapter would be a good way to go
about it. My only concern is: my DLL will be receiving the csv data in
a stream in the future instead of picking it up from a file... Does
this mean I'd have to save the stream to a file then use the OleDb
adapter to load it?

Thanks,
Thiago
Thiago,

I would actually use the classes in the System.Data.OleDb namespace for
this. Check out the following thread on MSDN forums, it should give you a
good start:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=6015&SiteID=1

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi all,

I have a csv file exported from excel. On cells where there was a comma
in the text, it wraps the character with double quotes. For example:

Column 1,"column two, and some more stuff",Column 3

In a c# dll, .NET framework 1.1, I'm reading the file using a
streamreader then retrieving each line into a string using
reader.ReadLine().

If I use the normal string.Split(',') it ends up splitting the line
into 4 strings:
Column 1
"column two
and some more stuff"
Column 3

Whereas I need it to be split into this:
Column 1
"column two, and some more stuff"
Column 3

Is there a proven and efficient method of splitting the line but
keeping the wrapper character in mind?

Cheers,
Thiago
 
This here seems to do the trick:
http://www.codeproject.com/cs/database/CsvReader.asp

Thank you for the replies.

Thiago
Yes, it's a limitation of both the jet driver and the odbc driver that
the data must be in an action file.

Optionally, you can check out the commercial product that I sell,
http://www.csvreader.com .

Bruce Dunwiddie

Hmm, I had a look at that forum, pretty interesting stuff. It looks
like using the Microsoft Jet OleDb adapter would be a good way to go
about it. My only concern is: my DLL will be receiving the csv data in
a stream in the future instead of picking it up from a file... Does
this mean I'd have to save the stream to a file then use the OleDb
adapter to load it?

Thanks,
Thiago
Thiago,

I would actually use the classes in the System.Data.OleDb namespace for
this. Check out the following thread on MSDN forums, it should give you a
good start:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=6015&SiteID=1

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi all,

I have a csv file exported from excel. On cells where there was a comma
in the text, it wraps the character with double quotes. For example:

Column 1,"column two, and some more stuff",Column 3

In a c# dll, .NET framework 1.1, I'm reading the file using a
streamreader then retrieving each line into a string using
reader.ReadLine().

If I use the normal string.Split(',') it ends up splitting the line
into 4 strings:
Column 1
"column two
and some more stuff"
Column 3

Whereas I need it to be split into this:
Column 1
"column two, and some more stuff"
Column 3

Is there a proven and efficient method of splitting the line but
keeping the wrapper character in mind?

Cheers,
Thiago
 
Back
Top