A Simple CString Tokenizer
Posted
by Richard Case
on March 3rd, 1999
As the name suggests, this is a simple class to extract tokens from a CSting. I wrote this class becuase during the course of my final year project at University I needed a simple way to extract 'tokens' from a CString. This is what I came up with. Its probably not the best or most effeicent way to accomplish the task - but it works which in the main thing.
To use the CToken class:
-First include the CToken header file in your porgram:
#include "Token.h"
-Then you are free to create and use an instance of CToken. For example:
CString str = "A B C D"
CString newTok;
CToken tok(str);
tok.SetToken(" ");
while(tok.MoreTokens())
{
newTok = tok.GetToken()
}
Improvements:If people can suggest any improvements or there bugs please drop me an email

Comments
More concessions with herveleger, more stagger!
Posted by wolemrfqcm on 04/29/2013 08:39pmwenchpicayune shaverelationpurloinmaterialmarket in
ReplyNike Draught Max+instagram, wishes you contain the color to bear up on your feet!
Posted by madytreathy on 04/23/2013 09:13pmRemember in 2008, if not earlier, when Nike launched winning of the separated shoe color projects, the catchword "Whiz Your Colours", "Nike PhotoiD" blueprint, [url=http://northernroofing.co.uk/roofins.cfm]nike free run[/url] response has not been as avid as expected. Think, 2008 Canon IXUS 80 IS Digital greetings card arcade but purely 8 million pixels, Nokia, the plastic phone market-place is the one leadership, NikeiD was promote to color in the photos as a infrastructure quest of sneakers duty color, although gripping, but does bother some. Instagram which cause this thing fun and simple, Nike PHOTOiD homeopathic upgrade customization services, recently [url=http://markwarren.org.uk/goodbuy.cfm]nike free uk[/url] released a strange plan. That such iD can you realize pictures as instagram account shoe color, for a short offer Nike Mood Max shoes and Nike Refresh Max 1, Nike Air Max 90 953 options. Interested in children's shoes, you [url=http://northernroofing.co.uk/roofins.cfm]nike free run 3[/url] can ever conform with each other's legitimate website photoid.Nike.com, in beyond to flip other people's artistic work, or you can make an effort to upload your own instagram photo, erect your own Nike Air Max.
Replyhelp
Posted by umar on 01/25/2013 02:11amhi, I am a beginner in C++. And i am very thankful to you if answer my question kindly. i want to know that if i save a file with a lot of data using ofstream then if i want to find a specific data from whole file. who can i do it.How can i make a struct or function for it. what is the coding please tell me.
Replyproblem: blank tokens are trimmed!
Posted by pamar on 11/15/2007 11:35amWhit this string: "A|B|C| " and | separator, last token must be " " but it is trimmed.
Replyi rewrote it : shorter & better
Posted by Legacy on 03/10/2003 12:00amOriginally posted by: joeri
just for those in need, i thought this delimiter wasn't working well, so i rewrote it:
class CToken
{
public:
virtual CString GetNextToken(); // Returns the next token
virtual BOOL MoreTokens(); // Do we have any more tokens to retrieve
int m_nAmount;
int m_nCurrent;
CToken(CString str,CString token=" ");
virtual ~CToken();
protected:
CString m_strToToken; // The original string to tokenize
CString m_strLeft; // The string we have left after taking tokens out
CString m_strToken; // What is the value to tokenize on?
private:
int GetAmount(CString str);
};
CToken::CToken(CString str, CString token)
{
str.TrimLeft();
str.TrimRight();
m_strToToken = str;
m_strLeft = str;
m_strToken = token;
m_nAmount = GetAmount(str);
m_nCurrent = 0;
}
CToken::~CToken()
{
}
int CToken::GetAmount(CString str)
{
int count = 0;
int pos, len;
len = str.GetLength();
pos = str.Find(m_strToken,0);
while (pos != -1)
{
count++;
pos++;
str = str.Right(len - pos);
str.TrimLeft();
len = str.GetLength();
pos = str.Find(m_strToken,0);
}
if (!str.IsEmpty()) count++;
return count;
}
BOOL CToken::MoreTokens()
{
if (m_nCurrent < m_nAmount)
return TRUE;
else return FALSE;
}
ReplyCString CToken::GetNextToken()
{
if(!MoreTokens())
return "";
CString ret;
int pos, len;
len = m_strLeft.GetLength();
pos = m_strLeft.Find(m_strToken,0);
if (pos != -1)
{
ret = m_strLeft.Left(pos);
pos++;
m_strLeft = m_strLeft.Right(len - pos);
m_strLeft.TrimLeft();
}
else ret = m_strLeft;
m_nCurrent++;
return ret;}
lexiacal analyzing
Posted by Legacy on 01/12/2003 12:00amOriginally posted by: alan
ReplyNice class
Posted by Legacy on 07/31/2002 12:00amOriginally posted by: Annette
Hi,
I just wanted to say that even if it a simple class, I thought it was very nice and helpful. I like working with CString instead of "hacking" along with all these char * and other string types.
So, thanks!!
Annette Skaar
Replypointer to a pointer in a string class
Posted by Legacy on 05/06/2001 12:00amOriginally posted by: Cris Moore
I'm working on a project. My data member in the class is char **list. The parameter I'm being passed is char *alist. How do I convert?
ReplyI need help!!
Posted by Legacy on 04/18/2001 12:00amOriginally posted by: John
I am a beginner in visual C++ programming.I have to do a program that read a multi-command file . Each command have a fixed number of parameters .I have to make a string analysis and based on that analysis the program should fill some small records like matrix (two dimension vectors).
What would be the best way to do this?(in special the string analysis).
ReplyI wouldn't bother using this class....
Posted by Legacy on 09/06/2000 12:00amOriginally posted by: Paul Wendt
Hey,
ReplyThis class doesn't really offer anything above strtok so I'd personally stick with strtok. It's not like there is a huge learning curve with learning strtok either :)
--paul wendt
Loading, Please Wait ...