CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    [RESOLVED] String.Split

    I'm having problems with parsing report data I get from 3rd party service.

    The data is delivered in CSV format
    Code:
    Netherlands,5124.15,some more values
    United States,102.10,some more values
    using the String.Split method (and comma as seperator), reading this is not a problem.

    But now I found the data like this
    Code:
    Netherlands,5124.15,some more values
    "Congo, the Replublic of",123.45,some more
    Parsing the second line result that the country is split into 2.

    How can I prevent this? Or should I use regex instead of .Split method?

    Things different in the second line:
    - The name of the country is included with quotes
    - After the comma in the country name is a space, between the values there's not.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: String.Split

    I've always taken the csv reader approach...

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

    From the article intro:

    One would imagine that parsing CSV files is a straightforward and boring task. I was thinking that too, until I had to parse several CSV files of a couple GB each. After trying to use the OLEDB JET driver and various Regular Expressions, I still ran into serious performance problems. At this point, I decided I would try the custom class option. I scoured the net for existing code, but finding a correct, fast, and efficient CSV parser and reader is not so simple, whatever platform/language you fancy.

    I say correct in the sense that many implementations merely use some splitting method like String.Split(). This will, obviously, not handle field values with commas. Better implementations may care about escaped quotes, trimming spaces before and after fields, etc., but none I found were doing it all, and more importantly, in a fast and efficient manner.

    And, this led to the CSV reader class I present in this article. Its design is based on the System.IO.StreamReader class, and so is a non-cached, forward-only reader (similar to what is sometimes called a fire-hose cursor).

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: String.Split

    Sorry for my late respone.

    The CsvReader seems to works :-)

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured