Click here to Skip to main content
Click here to Skip to main content

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

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)

Share

About the Author

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

Comments and Discussions

 
GeneralRuntime error wiwhen compiled with visual 2005 Pinmemberzzdomi26-Feb-09 3:08 
Question"more than one string at the time"? Pinmemberl_d_allan28-Feb-06 6:24 
AnswerRe: "more than one string at the time"? PinmemberDViggiano10-Jul-07 6:51 
GeneralPls provide test program Pinmemberl_d_allan28-Feb-06 6:17 
GeneralMemleakage Pinmemberspinoza11-May-05 5:51 
GeneralRe: Memleakage PinmemberEddie Velasquez11-May-05 6:21 
GeneralNull values Pinmemberhitnrun16-Dec-03 5:59 
GeneralRe: Null values PinmemberEddie Velasquez16-Dec-03 6:30 
GeneralRe: Null values Pinmemberhitnrun16-Dec-03 7:02 
GeneralRe: Null values PinmemberEddie Velasquez16-Dec-03 8:49 
GeneralRe: Null values Pinmemberhitnrun17-Dec-03 5:42 
GeneralRe: Null values PinmemberEddie Velasquez17-Dec-03 5:53 
GeneralThis won't work for UNICODE! PinmemberMustafa Demirhan21-Apr-03 22:29 
GeneralRe: This won't work for UNICODE! PinmemberEddie Velasquez22-Apr-03 8:19 
GeneralBug! PinmemberAnonymous24-Sep-01 0:51 
GeneralRe: Bug! PinmemberAnonymous25-Sep-01 3:49 
GeneralRe: Bug! - Solution ? PinmemberJohn Simpsons22-Oct-01 3:24 
GeneralRe: Bug! PinmemberEddie Velasquez22-Oct-01 4:28 
GeneralRe: Bug! PinmemberJohn Simpsons22-Oct-01 5:00 
GeneralRe: Bug! PinmemberEddie Velasquez22-Oct-01 5:11 
GeneralRe: Bug! PinmemberEddie Velasquez22-Oct-01 6:54 
GeneralRe: Bug! PinmemberEddie Velasquez22-Oct-01 7:36 
GeneralRe: Bug! Pinmemberdead_alive8-Dec-03 5:51 
GeneralThanks! PinmemberDaniel Turini20-Sep-01 4:24 
GeneralCan't download PinmemberBruce Hearder25-Aug-01 5:08 
GeneralTail didn't work with '\' PinsussTrey24-Jul-00 6:13 
GeneralRe: Tail didn't work with '\' PinsussEddie Velasquez24-Jul-00 12:50 
GeneralRe: Tail didn't work with '\' PinsussTrey25-Jul-00 7:39 
GeneralRe: Tail didn't work with '\' PinmemberJohn Simmons / outlaw programmer15-Oct-01 4:56 
GeneralIncluding vector PinsussEddie Velasquez13-Jan-00 4:06 
GeneralRe: Including vector PinsussEddie Velasquez13-Jan-00 4:13 
GeneralNot really the comment script (was: Including vector) PinsussUwe Keim13-Jan-00 10:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.150331.1 | Last Updated 22 Oct 2001
Article Copyright 1999 by Eddie Velasquez
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid