string extraction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    string extraction


    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::prin t_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;
    }
  • rep_movsd

    #2
    Re: string extraction


    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::tokenize r 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

    Comment

    • ondra.holub

      #3
      Re: string extraction

      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(con st std::string& s, char split_char = ',')
      {
      short itm = 0;
      std::string::si ze_type from = 0;

      for (;;)
      {
      std::string::si ze_type pos = s.find(split_ch ar, from);
      std::cout << "[" << itm << "] ";

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

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

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

      std::cout << "[0] ";
      for (std::string::c onst_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.,gh j.,my name is");

      std::cout << SplitString(s) << "\n";
      std::cout << "---------\n";
      std::cout << SplitString2(s) << "\n";
      }

      Comment

      • dragan.jovev@gmail.com

        #4
        Re: string extraction

        Why not use strtok from string.h? This example shows exactly what you
        want to achieve:


        Best reguards,
        Dragan

        On Nov 14, 11:35 am, Gary Wessle <phd...@yahoo.c omwrote:
        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::prin t_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;
        }

        Comment

        • terminator

          #5
          Re: string extraction



          On Nov 14, 1:59 pm, "ondra.holu b" <ondra.ho...@po st.czwrote:
          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(con st std::string& s, char split_char = ',')
          {
          short itm = 0;
          std::string::si ze_type from = 0;
          >
          for (;;)
          {
          std::string::si ze_type pos = s.find(split_ch ar, from);
          std::cout << "[" << itm << "] ";
          >
          if (pos == std::string::np os)
          {
          std::cout << s.substr(from) << "\n";
          return itm;
          }
          >
          std::cout << s.substr(from, pos - from) << "\n";
          from = pos + 1;
          ++itm;
          }
          >
          }short SplitString2(co nst std::string& s, char split_char = ',')
          {
          short itm = 0;
          >
          std::cout << "[0] ";
          for (std::string::c onst_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.,gh j.,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'?

          Comment

          • ondra.holub

            #6
            Re: string extraction

            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.

            Comment

            • BobR

              #7
              Re: string extraction


              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::prin t_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::ostream &out){
              std::string line;
              while( std::getline( in, line, ',' ) ){
              out << line << std::endl;
              }
              return;
              } // SplitPrint(istr eam&, ostream&)

              int main() // or function
              {
              std::cout<<"--- SplitPrint test"<<std::end l;
              // std::string TheLine( "abc.,def.,ghj. ,my name is" );
              // std::istringstr eam sis( TheLine );
              // SplitPrint( sis, std::cout );
              // std::ofstream sofs("MyFile.tx t");
              // SplitPrint( sis, sofs );

              std::istringstr eam sis( "abc.,def.,ghj. ,my name is" );
              std::ostringstr eam sos;
              SplitPrint( sis, sos );
              std::cout<<sos. str()<<std::end l;
              std::cout<<"--- SplitPrint test end"<<std::endl ;
              }

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

              Better?
              --
              Bob R
              POVrookie


              Comment

              • BobR

                #8
                Re: string extraction


                BobR wrote in message ...
                >
                >#include <iostream>
                >#include <ostream>
                >#include <sstream>
                >
                void SplitPrint( std::istream &in, std::ostream &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(istr eam&, ostream&)

                --
                Bob R
                POVrookie


                Comment

                Working...