Click here to Skip to main content
15,881,044 members
Articles / Mobile Apps / Windows Mobile
Article

CTokenizer class

Rate me:
Please Sign up or sign in to vote.
3.82/5 (10 votes)
21 Oct 2001CPOL 161.8K   1.4K   38   32
A simple tokenizer class that can be used on CStrings

Introduction

Sometimes you need to use strtok in MFC projects, but it has two big limitations:

  • Can't use it to tokenize more than one string at the time.
  • It's not easy to use safely with CStrings

To overcome these limitations I wrote CTokenizer.

Usage

It's quite easy to use as the following code shows:

CTokenizer tok(_T("A-B+C*D-E"), _T("-+"));
CString cs;

while(tok.Next(cs))
    TRACE2("Token: '%s', Tail: '%s'\n", (LPCTSTR)cs, (LPCTSTR)tok.Tail());

The preceding code produces the following output:

Token: 'A', Tail: 'B+C*D-E'
Token: 'B', Tail: 'C*D-E'
Token: 'C*D', Tail: 'E'
Token: 'E', Tail: ''

As you can see, it's no rocket science! It's a very simple and handy class

To summarize:

  • Create an object of type CTokenizer, passing as arguments the string to tokenize and the delimiters
  • Call Next() until it returns false
  • If you need to change the delimiters, call SetDelimiters()
  • If you need the un-tokenized part of the string, call Tail()

Updates

  • 10/22/2001 Using std::bitset instead of std::vector. Reduces memory requirements and increases performance.
  • 10/22/2001 Fixed bug related to ASCII characters above 127. Reported by John Simpsons

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRuntime error wiwhen compiled with visual 2005 Pin
zzdomi26-Feb-09 2:08
zzdomi26-Feb-09 2:08 
Question"more than one string at the time"? Pin
l_d_allan28-Feb-06 5:24
l_d_allan28-Feb-06 5:24 
AnswerRe: "more than one string at the time"? Pin
DViggiano10-Jul-07 5:51
DViggiano10-Jul-07 5:51 
GeneralPls provide test program Pin
l_d_allan28-Feb-06 5:17
l_d_allan28-Feb-06 5:17 
GeneralMemleakage Pin
spinoza11-May-05 4:51
spinoza11-May-05 4:51 
GeneralRe: Memleakage Pin
Eddie Velasquez11-May-05 5:21
Eddie Velasquez11-May-05 5:21 
GeneralNull values Pin
hitnrun16-Dec-03 4:59
hitnrun16-Dec-03 4:59 
GeneralRe: Null values Pin
Eddie Velasquez16-Dec-03 5:30
Eddie Velasquez16-Dec-03 5:30 
GeneralRe: Null values Pin
hitnrun16-Dec-03 6:02
hitnrun16-Dec-03 6:02 
GeneralRe: Null values Pin
Eddie Velasquez16-Dec-03 7:49
Eddie Velasquez16-Dec-03 7:49 
GeneralRe: Null values Pin
hitnrun17-Dec-03 4:42
hitnrun17-Dec-03 4:42 
GeneralRe: Null values Pin
Eddie Velasquez17-Dec-03 4:53
Eddie Velasquez17-Dec-03 4:53 
GeneralThis won't work for UNICODE! Pin
Mustafa Demirhan21-Apr-03 21:29
Mustafa Demirhan21-Apr-03 21:29 
GeneralRe: This won't work for UNICODE! Pin
Eddie Velasquez22-Apr-03 7:19
Eddie Velasquez22-Apr-03 7:19 
GeneralBug! Pin
23-Sep-01 23:51
suss23-Sep-01 23:51 
GeneralRe: Bug! Pin
25-Sep-01 2:49
suss25-Sep-01 2:49 
GeneralRe: Bug! - Solution ? Pin
22-Oct-01 2:24
suss22-Oct-01 2:24 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 3:28
Eddie Velasquez22-Oct-01 3:28 
GeneralRe: Bug! Pin
22-Oct-01 4:00
suss22-Oct-01 4:00 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 4:11
Eddie Velasquez22-Oct-01 4:11 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 5:54
Eddie Velasquez22-Oct-01 5:54 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 6:36
Eddie Velasquez22-Oct-01 6:36 
GeneralRe: Bug! Pin
dead_alive8-Dec-03 4:51
dead_alive8-Dec-03 4:51 
GeneralThanks! Pin
Daniel Turini20-Sep-01 3:24
Daniel Turini20-Sep-01 3:24 
Just what I needed!
Good work!


Furor fit laesa saepius patientia
GeneralCan't download Pin
Bruce Hearder25-Aug-01 4:08
Bruce Hearder25-Aug-01 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.