string extraction

Discussion in 'C++' started by Gary Wessle, Nov 14, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi
    given a coma delimited string "abc.,def.,ghj.,my name is", I need to
    get the output

    //output
    abc.
    def.
    jhj.
    my name is


    here is how I did it but it is messy. I hope someone came up with a
    cleaner version.

    thank you

    short Conn_task::print_smi(const string& s){
    short itm = 0;
    int from =0;
    int sz = 0;
    while( from <= s.rfind(",") ){
    if( sz = s.find(",", from) )
    cout << "[" << itm << "] " << s.substr(from, sz-from) << endl;
    from =sz+1;
    ++itm;
    }
    cout << itm << s.substr(from, s.length()-from) << endl;
    }
     
    Gary Wessle, Nov 14, 2006
    #1
    1. Advertising

  2. Gary Wessle

    rep_movsd Guest

    Gary Wessle wrote:
    > Hi
    > given a coma delimited string "abc.,def.,ghj.,my name is", I need to
    > get the output
    >
    > //output
    > abc.
    > def.
    > jhj.
    > my name is
    >


    boost::tokenizer is ideal for this.
    Look at the boost library, its almost part of the C++ standard and has
    all the most important reinvented wheels.

    Regards
    Vivek
     
    rep_movsd, Nov 14, 2006
    #2
    1. Advertising

  3. Gary Wessle

    ondra.holub Guest

    Here are two versions. First one is slightly modified your version, the
    second is with iterato. You can use any of them which you like:

    #include <string>
    #include <iostream>

    short SplitString(const std::string& s, char split_char = ',')
    {
    short itm = 0;
    std::string::size_type from = 0;

    for (;;)
    {
    std::string::size_type pos = s.find(split_char, from);
    std::cout << "[" << itm << "] ";

    if (pos == std::string::npos)
    {
    std::cout << s.substr(from) << "\n";
    return itm;
    }

    std::cout << s.substr(from, pos - from) << "\n";
    from = pos + 1;
    ++itm;
    }
    }

    short SplitString2(const std::string& s, char split_char = ',')
    {
    short itm = 0;

    std::cout << "[0] ";
    for (std::string::const_iterator pos = s.begin(); pos != s.end();
    ++pos)
    {
    if (*pos == split_char)
    std::cout << "\n[" << ++itm << "] ";
    else
    std::cout << *pos;
    }

    std::cout << "\n";
    return itm;
    }

    int main()
    {
    std::string s("abc.,def.,ghj.,my name is");

    std::cout << SplitString(s) << "\n";
    std::cout << "---------\n";
    std::cout << SplitString2(s) << "\n";
    }
     
    ondra.holub, Nov 14, 2006
    #3
  4. Gary Wessle

    Guest

    Why not use strtok from string.h? This example shows exactly what you
    want to achieve:
    http://www.cplusplus.com/ref/cstring/strtok.html

    Best reguards,
    Dragan

    On Nov 14, 11:35 am, Gary Wessle <> wrote:
    > Hi
    > given a coma delimited string "abc.,def.,ghj.,my name is", I need to
    > get the output
    >
    > //output
    > abc.
    > def.
    > jhj.
    > my name is
    >
    > here is how I did it but it is messy. I hope someone came up with a
    > cleaner version.
    >
    > thank you
    >
    > short Conn_task::print_smi(const string& s){
    > short itm = 0;
    > int from =0;
    > int sz = 0;
    > while( from <= s.rfind(",") ){
    > if( sz = s.find(",", from) )
    > cout << "[" << itm << "] " << s.substr(from, sz-from) << endl;
    > from =sz+1;
    > ++itm;
    > }
    > cout << itm << s.substr(from, s.length()-from) << endl;
    > }
     
    , Nov 14, 2006
    #4
  5. Gary Wessle

    terminator Guest

    On Nov 14, 1:59 pm, "ondra.holub" <> wrote:
    > Here are two versions. First one is slightly modified your version, the
    > second is with iterato. You can use any of them which you like:
    >
    > #include <string>
    > #include <iostream>
    >
    > short SplitString(const std::string& s, char split_char = ',')
    > {
    > short itm = 0;
    > std::string::size_type from = 0;
    >
    > for (;;)
    > {
    > std::string::size_type pos = s.find(split_char, from);
    > std::cout << "[" << itm << "] ";
    >
    > if (pos == std::string::npos)
    > {
    > std::cout << s.substr(from) << "\n";
    > return itm;
    > }
    >
    > std::cout << s.substr(from, pos - from) << "\n";
    > from = pos + 1;
    > ++itm;
    > }
    >
    > }short SplitString2(const std::string& s, char split_char = ',')
    > {
    > short itm = 0;
    >
    > std::cout << "[0] ";
    > for (std::string::const_iterator pos = s.begin(); pos != s.end();
    > ++pos)
    > {
    > if (*pos == split_char)
    > std::cout << "\n[" << ++itm << "] ";
    > else
    > std::cout << *pos;
    > }
    >
    > std::cout << "\n";
    > return itm;
    >
    > }int main()
    > {
    > std::string s("abc.,def.,ghj.,my name is");
    >
    > std::cout << SplitString(s) << "\n";
    > std::cout << "---------\n";
    > std::cout << SplitString2(s) << "\n";
    >
    >
    >
    > }- Hide quoted text -- Show quoted text -




    is it not possible to replace comas in the original string with zeros
    and then sending it to 'cout'?
     
    terminator, Nov 14, 2006
    #5
  6. Gary Wessle

    ondra.holub Guest

    Yes, it is definitely possible (I would suggest to replace it with '\n'
    character to get required output) as long as you can modify (and
    destroy) content of string. Then it is simillar to use of strtok.
     
    ondra.holub, Nov 14, 2006
    #6
  7. Gary Wessle

    BobR Guest

    Gary Wessle wrote in message ...
    >
    >Hi
    >given a coma delimited string "abc.,def.,ghj.,my name is", I need to
    >get the output
    >
    >//output
    >abc.
    >def.
    >jhj.
    >my name is
    >
    >here is how I did it but it is messy. I hope someone came up with a
    >cleaner version.
    >thank you
    >
    >short Conn_task::print_smi(const string& s){
    > short itm = 0;
    > int from =0;
    > int sz = 0;
    > while( from <= s.rfind(",") ){
    > if( sz = s.find(",", from) )
    > cout << "[" << itm << "] " << s.substr(from, sz-from) << endl;
    > from =sz+1;
    > ++itm;
    > }
    > cout << itm << s.substr(from, s.length()-from) << endl;
    > }


    #include <iostream>
    #include <ostream>
    #include <sstream>

    void SplitPrint( std::istream &in, std::eek:stream &out){
    std::string line;
    while( std::getline( in, line, ',' ) ){
    out << line << std::endl;
    }
    return;
    } // SplitPrint(istream&, ostream&)

    int main() // or function
    {
    std::cout<<"--- SplitPrint test"<<std::endl;
    // std::string TheLine( "abc.,def.,ghj.,my name is" );
    // std::istringstream sis( TheLine );
    // SplitPrint( sis, std::cout );
    // std::eek:fstream sofs("MyFile.txt");
    // SplitPrint( sis, sofs );

    std::istringstream sis( "abc.,def.,ghj.,my name is" );
    std::eek:stringstream sos;
    SplitPrint( sis, sos );
    std::cout<<sos.str()<<std::endl;
    std::cout<<"--- SplitPrint test end"<<std::endl;
    }

    /* --- SplitPrint test
    abc.
    def.
    ghj.
    my name is
    --- SplitPrint test end
    */

    Better?
    --
    Bob R
    POVrookie
     
    BobR, Nov 14, 2006
    #7
  8. Gary Wessle

    BobR Guest

    BobR wrote in message ...
    >
    >#include <iostream>
    >#include <ostream>
    >#include <sstream>
    >

    void SplitPrint( std::istream &in, std::eek:stream &out){
    //std::string line;
    //while( std::getline(in, line, ',') ){
    // out << line << std::endl;
    // }

    for( std::string line; std::getline( in, line, ',' ); ){
    out << line << std::endl;
    }

    return;
    } // SplitPrint(istream&, ostream&)

    --
    Bob R
    POVrookie
     
    BobR, Nov 14, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Di'ego

    Clock and data extraction

    Di'ego, Dec 21, 2003, in forum: VHDL
    Replies:
    0
    Views:
    470
    Di'ego
    Dec 21, 2003
  2. Tony Smith
    Replies:
    0
    Views:
    806
    Tony Smith
    Apr 28, 2004
  3. Navindra Umanee

    string extraction

    Navindra Umanee, Jan 29, 2005, in forum: Ruby
    Replies:
    6
    Views:
    115
    Bertram Scharpf
    Jan 29, 2005
  4. Li Chen

    string extraction

    Li Chen, Jan 10, 2008, in forum: Ruby
    Replies:
    8
    Views:
    103
    James Gray
    Jan 10, 2008
  5. Unmesh Gundecha

    String extraction using RegExp

    Unmesh Gundecha, Jun 9, 2008, in forum: Ruby
    Replies:
    2
    Views:
    95
    Unmesh Gundecha
    Jun 9, 2008
Loading...

Share This Page