Click here to Skip to main content
Licence CPOL
First Posted 3 Dec 1999
Views 123,323
Bookmarked 36 times

CTokenizer class

By | 21 Oct 2001 | Article
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)

About the Author

Eddie Velasquez

Software Developer (Senior)

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRuntime error wiwhen compiled with visual 2005 Pinmemberzzdomi2:08 26 Feb '09  
Question"more than one string at the time"? Pinmemberl_d_allan5:24 28 Feb '06  
AnswerRe: "more than one string at the time"? PinmemberDViggiano5:51 10 Jul '07  
GeneralPls provide test program Pinmemberl_d_allan5:17 28 Feb '06  
GeneralMemleakage Pinmemberspinoza4:51 11 May '05  
GeneralRe: Memleakage PinmemberEddie Velasquez5:21 11 May '05  
GeneralNull values Pinmemberhitnrun4:59 16 Dec '03  
GeneralRe: Null values PinmemberEddie Velasquez5:30 16 Dec '03  
GeneralRe: Null values Pinmemberhitnrun6:02 16 Dec '03  
GeneralRe: Null values PinmemberEddie Velasquez7:49 16 Dec '03  
GeneralRe: Null values Pinmemberhitnrun4:42 17 Dec '03  
GeneralRe: Null values PinmemberEddie Velasquez4:53 17 Dec '03  
GeneralThis won't work for UNICODE! PinmemberMustafa Demirhan21:29 21 Apr '03  
GeneralRe: This won't work for UNICODE! PinmemberEddie Velasquez7:19 22 Apr '03  
GeneralBug! PinmemberAnonymous23:51 23 Sep '01  
GeneralRe: Bug! PinmemberAnonymous2:49 25 Sep '01  
GeneralRe: Bug! - Solution ? PinmemberJohn Simpsons2:24 22 Oct '01  
GeneralRe: Bug! PinmemberEddie Velasquez3:28 22 Oct '01  
GeneralRe: Bug! PinmemberJohn Simpsons4:00 22 Oct '01  
GeneralRe: Bug! PinmemberEddie Velasquez4:11 22 Oct '01  
GeneralRe: Bug! PinmemberEddie Velasquez5:54 22 Oct '01  
GeneralRe: Bug! PinmemberEddie Velasquez6:36 22 Oct '01  
GeneralRe: Bug! Pinmemberdead_alive4:51 8 Dec '03  
GeneralThanks! PinmemberDaniel Turini3:24 20 Sep '01  
GeneralCan't download PinmemberBruce Hearder4:08 25 Aug '01  

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.

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