Personally I cheat and use strtok().
Main Topics
Browse All TopicsI am trying to tokenize a CString and was using VC++6 at home while at work I am using VC++5. I spent a lot of time at home coming up with this algorithm to tokenize a CString that looks like this:
2.0,3.0,4.0 (delimiters can be commas or spaces.)
before I realized that I the CString functions have changed in the newest version of MFC. Is there a way I can use a similar alogorithm with the limitation of being able to use only CString::Find(char ch) that accepts just the char you are looking for and not the starting position as well? If not, how could I tokenize a CString to grab these data points?
Here is a snippet of my code:
//.......
while(r < return_data.GetLength()) {
if(return_data.Find("," || " ", r) != -1){
index = return_data.Find("," || " ",r);
data_points[w] = return_data.Mid(r,index-r)
r = index+1;
}
else {
data_points[w] = return_data.Mid(r,return_d
r = return_data.GetLength();
}
w++;
//......
Thanks for your help.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>if(return_data.Find("," || " ", r) != -1){
>index = return_data.Find("," || " ",r);
You cannot use || like this ... it just WONT work that way, and NEVER would have.
Use FindOneOf instead of Find to do this eg. FindOneOf(", ") .. however, FindOneOf doesn't iunclude a starting position.
Also, look at SpanExcluding and SpanIncluding which give you a (leading) substring bounded by tokens or containing certain characters respectively.
If all you want to do is read numbers that are comma separated, just use sscanf instead, or use std C++ streams and use <<.
-I know that
>if(return_data.Find("," || " ", r) != -1){
>index = return_data.Find("," || " ",r);
wouldn't work . . .I just put those in as a mental note and forgot to take out before I posted it, sorry.
-FindOneOf() doesn't do me much good if it doesn't have a starting position. I can just use Find()
-either does spanIncluding()
-I want to return CStrings . . .
I think my best bet is to do something like mandhjo suggested . . .
The span functions are nicest. You could use them like this...
CString RemoveTokenWithSeparators(
CString token = string.SpanExcluding(chars
string = string.Mid(token.GetLength
return token;
}
CString RemoveTokenFromCharset(CSt
CString token = string.SpanIncluding(chars
string = string.Mid(token.GetLength
return token;
}
Now you could say
CString s = "1.0,2.0,3.0";
while (! s.IsEmpty()) {
CString token = s.RemoveTokenWithSeparator
...
}
missed a couple of commas there .. but you should get the idea.
>-I know that
>>if(return_data.Find("," || " ", r)
>>index = return_data.Find("," || " ",r);
>wouldn't work . . .I just put those in
>as a mental note and forgot to take
>out before I posted it, sorry.
Maybe you should edit the question accordingly?
>-FindOneOf() doesn't do me much good
>if it doesn't have a starting
>position. I can just use Find()
No, you cannot use find in that case, as find only look for a particular substring, whereas FindOneOf looks for a character from a set of characters .. very different. You'd need to write quite a bit of code to emulate FindOneOf using Find.
>-either does spanIncluding()
But it is probably the best for what you want to do (ie. break up based on tokens). Either that or using FindOneOf.
>-I want to return CStrings . . .
See my new answer
Business Accounts
Answer for Membership
by: mandhjoPosted on 2000-07-18 at 09:11:29ID: 3449312
how about set return_date = to the remaining string after the last token removed?
ata.GetLen gth() - (index + 1));
Place something like the following right before "r = index + 1"
return_data = return_data.Right(return_d
As the data is searched, the string that is searched becomes smaller and smaller.
First Time through - 2.0,3.0,4.0
Next Time - 3.0,4.0
Last Time - 4.0
Make sense?