Parsing comma seperated string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Parsing comma seperated string

  1. #1
    Jim Osborne Guest

    Parsing comma seperated string


    Can anybody tell me why this does not work or suggest a better way to parse
    a CVS string? The find() function works fine except when it is cascaded on
    the same string!!! Thanks...

    #include <string>

    std::string str = "Stroustrup, 8005551212, etc, etc";
    std::string name, number, etc, etc;
    int i, ii;

    i = str.find(',',0);
    name = str.substr(0,i);

    ii = str.find(',',i+1);
    number = str.substr(i+1,ii);

    etc...

    etc..


  2. #2
    Dave Guest

    Re: Parsing comma seperated string


    "Jim Osborne" <sundownr@sprynet.com> wrote:
    >
    >Can anybody tell me why this does not work or suggest a better way to parse
    >a CVS string? The find() function works fine except when it is cascaded

    on
    >the same string!!! Thanks...
    >
    >#include <string>
    >
    >std::string str = "Stroustrup, 8005551212, etc, etc";
    >std::string name, number, etc, etc;
    >int i, ii;
    >
    >i = str.find(',',0);
    >name = str.substr(0,i);
    >
    >ii = str.find(',',i+1);
    >number = str.substr(i+1,ii);
    >
    >etc...
    >
    >etc..
    >


    Jim, the problem is with substr not find. The second argument is a length
    specifier (element count) not a position identifier. The following would
    work better:

    ii = str.find(',',i+1);
    number = str.substr(i+2, ii-(i+2) );

    I personally like to parse with streams, especially if you can do without
    the commas:
    #include <sstream>

  3. #3
    Dave Guest

    Re: Parsing comma seperated string


    "Dave" <lank3403@my-deja.com> wrote:
    > s >> name >> number >> etc1, etc2;
    > name = name.substr( 0, name.length()-1 ); // strip comma
    > number = number.substr( 0, number.length()-1 );
    >


    sorry, I had a typo, this code should have been:
    s >> name >> number >> etc1 >> etc2;
    name.erase( name.find(","), 1 ); // strip comma
    number.erase( number.find(","), 1 ); // strip comma


  4. #4
    Wayne Mack Guest

    Re: Parsing comma seperated string


    Jim,

    I usually use a much more manual approach to string parsing, because I dislike
    two pass solutions. Everyone usually has their own favorite parse mechanism,
    but you can try something like:

    std::string theSource;

    const char * theWorkingSource;
    std::string theName;
    theName.reserve(NAME_MAX_SIZE); //optional step
    for(theWorkingSource = theSource.c_str();
    (*theWorkingSource != '\0') && (*theWorkingSource != ',');
    ++theWorkingSource)
    {
    theName.append(theWorkingSource, 1);
    }

    std::string theNumber;
    if(*theWorkingSource != '\0')
    {
    theNumber.reserve(NUMBER_MAX_SIZE); //optional step
    for(++theWokringSource;
    (*theWorkingSource != '\0') && (*theWorkingSource != ',');
    ++theWorkingSource)
    {
    theNumber.append(theWorkingSouce, 1);
    }
    }

    // repeat as necessary.

    Wayne Mack
    PEC Solutions, Inc.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center