-
October 4th, 2005, 03:35 AM
#1
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)
-
October 4th, 2005, 03:43 AM
#2
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
-
October 4th, 2005, 04:01 AM
#3
Re: Tokenize with string
Thnx, I didn't know that function (this is my first C++ project).
Now I think I'll manage...
-
October 4th, 2005, 04:20 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|