CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Tokenize with string

  1. #1
    Join Date
    Oct 2005
    Location
    The Netherlands
    Posts
    10

    Tokenize with string

    Hello, is there a way to tokenize a string with a string instead of with a character?

    When i try to tokenize with, let's say "<==>", it only tokenize's with "<".

    This is the code I use;
    Token = strtok(data, "<==>" );

    (And I use Embedded Visual C++, to make a PDA application)

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Tokenize with string

    well for this you need to find a sub string.... look at strstr(..)
    but note its not working like strtok, you will need each iteration to build new string from the pos to you found the sub string till the end.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Oct 2005
    Location
    The Netherlands
    Posts
    10

    Re: Tokenize with string

    Thnx, I didn't know that function (this is my first C++ project).
    Now I think I'll manage...

  4. #4
    Join Date
    Aug 2002
    Posts
    879

    Re: Tokenize with string

    here is a simple tokenize function
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    Code:
    vector<string> tokenize(string s, string e) {
     vector<string> ret;
     int iPos = s.find(e, 0);
     int iPit = e.length();
     while(iPos>-1) {
       if(iPos!=0)
         ret.push_back(s.substr(0,iPos));
       s.erase(0,iPos+iPit);   
       iPos = s.find(e, 0);  
     }
     if(s!="")
       ret.push_back(s);
     return ret;
    }
    usage e.g.
    Code:
    	string str;
    	str = "111<==>222<==>333<==>444";
    	
            vector<string>	vStr;
    	vStr = tokenize(str,"<==>");
    Last edited by blueday54555; October 4th, 2005 at 04:24 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)