Click here to Skip to main content
12,689,801 members (23,128 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked

CTokenizer class

, 21 Oct 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple tokenizer class that can be used on CStrings


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.


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

CTokenizer tok(_T("A-B+C*D-E"), _T("-+"));
CString 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()


  • 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


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


About the Author

Eddie Velasquez
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRuntime error wiwhen compiled with visual 2005 Pin
zzdomi26-Feb-09 3:08
memberzzdomi26-Feb-09 3:08 
Question"more than one string at the time"? Pin
l_d_allan28-Feb-06 6:24
memberl_d_allan28-Feb-06 6:24 
AnswerRe: "more than one string at the time"? Pin
DViggiano10-Jul-07 6:51
memberDViggiano10-Jul-07 6:51 
GeneralPls provide test program Pin
l_d_allan28-Feb-06 6:17
memberl_d_allan28-Feb-06 6:17 
GeneralMemleakage Pin
spinoza11-May-05 5:51
memberspinoza11-May-05 5:51 
GeneralRe: Memleakage Pin
Eddie Velasquez11-May-05 6:21
memberEddie Velasquez11-May-05 6:21 
GeneralNull values Pin
hitnrun16-Dec-03 5:59
memberhitnrun16-Dec-03 5:59 
GeneralRe: Null values Pin
Eddie Velasquez16-Dec-03 6:30
memberEddie Velasquez16-Dec-03 6:30 
GeneralRe: Null values Pin
hitnrun16-Dec-03 7:02
memberhitnrun16-Dec-03 7:02 
GeneralRe: Null values Pin
Eddie Velasquez16-Dec-03 8:49
memberEddie Velasquez16-Dec-03 8:49 
GeneralRe: Null values Pin
hitnrun17-Dec-03 5:42
memberhitnrun17-Dec-03 5:42 
GeneralRe: Null values Pin
Eddie Velasquez17-Dec-03 5:53
memberEddie Velasquez17-Dec-03 5:53 
GeneralThis won't work for UNICODE! Pin
Mustafa Demirhan21-Apr-03 22:29
memberMustafa Demirhan21-Apr-03 22:29 
GeneralRe: This won't work for UNICODE! Pin
Eddie Velasquez22-Apr-03 8:19
memberEddie Velasquez22-Apr-03 8:19 
GeneralBug! Pin
Anonymous24-Sep-01 0:51
memberAnonymous24-Sep-01 0:51 
GeneralRe: Bug! Pin
Anonymous25-Sep-01 3:49
memberAnonymous25-Sep-01 3:49 
GeneralRe: Bug! - Solution ? Pin
John Simpsons22-Oct-01 3:24
memberJohn Simpsons22-Oct-01 3:24 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 4:28
memberEddie Velasquez22-Oct-01 4:28 
GeneralRe: Bug! Pin
John Simpsons22-Oct-01 5:00
memberJohn Simpsons22-Oct-01 5:00 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 5:11
memberEddie Velasquez22-Oct-01 5:11 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 6:54
memberEddie Velasquez22-Oct-01 6:54 
GeneralRe: Bug! Pin
Eddie Velasquez22-Oct-01 7:36
memberEddie Velasquez22-Oct-01 7:36 
GeneralRe: Bug! Pin
dead_alive8-Dec-03 5:51
memberdead_alive8-Dec-03 5:51 
GeneralThanks! Pin
Daniel Turini20-Sep-01 4:24
memberDaniel Turini20-Sep-01 4:24 
GeneralCan't download Pin
Bruce Hearder25-Aug-01 5:08
memberBruce Hearder25-Aug-01 5: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 22 Oct 2001
Article Copyright 1999 by Eddie Velasquez
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid