Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / MFC
Article

String Tokenizer Class (CTokenEx)

Rate me:
Please Sign up or sign in to vote.
4.66/5 (17 votes)
26 Jan 2000CPOL 106.3K   2.4K   30   15
A very simple string tokenizer class.

Introduction

Basically, I've seen other string tokenizers and they lacked the functionality I was looking for. Therefore, I created one for myself using the KISS (Keep-It-Simple-Stupid) method. This is a VERY SIMPLE sample!!!!

Here is a summary of the functionality in the CTokenEx class, you can:

  • use SplitPath to break-up the path into sections (Drive/Share name, Directory, Filename, Extension). Also, recognizes UNC names (which _tsplitpath doesn't).
  • use Join to create a CString from a CStringArray with delimiters of your choice.
  • use Split to break-up a CString into a CStringArray (according to the delimiter).
  • use GetString to get the first sub-string in a CString (according to the delimiter).

NOTE:

The Split and GetString functions recognize multiple delimiters as an empty string so that it will NOT add blanks to an array (unless you want it to). See example code below:

Say you have a CString that contains: "abc,def,,,ghi,,jkl,,"

//********************************************************
// Split Function
//********************************************************
//
// Split will fill an array with:
//
// NOTE:  IF PARAM #4 IS TRUE, YOU'LL SEE LIST #1 ELSE LIST #2
//
// LIST #1:
//  
// String  Position
// ======  ========
// abc     0
// def     1
//         2
//         3
// ghi     4
//         5
// jkl     6
//         7
//         8
//
//
// LIST #2 (Same String):
//  
// String  Position
// ======  ========
// abc     0
// def     1
// ghi     2
// jkl     3
//
//********************************************************
void <SOME NAME>Dlg::OnSplit() 
{
    CTokenEx tok;

    // CString for the Split Function
    CString csSplit = "abc,def,,,ghi,,jkl,,";

    // CStringArray to fill 
    CStringArray SplitIt; // Call Split
    tok.Split(csSplit, ",", SplitIt,  TRUE);  // LIST #1 
    tok.Split(csSplit, ",", SplitIt, FALSE);  // LIST #2 
}
  
/********************************************************
// GetString Function
//********************************************************  
// 
//  GetString will return a string:
// 
//     abc
//     ...and more calls to GetString will return a strings: 
//     def
//     ghi
//     jkl
//
//********************************************************
void <SOME NAME>Dlg::OnGetstring() 
{
    CTokenEx tok;  
    char Buf[254];  CString
    csRef = "abc,def,,,ghi,,jkl,,"; 
    do 
    {
        // don't return blanks
        CString csRet = tok.GetString(csRef, ",",  FALSE);
        //  return blanks
        CString csRet = tok.GetString(csRef, ",",  TRUE);

        // Do something with the returned value.

    } while (!csRef.IsEmpty());
}

I hope that others find this class useful.

License

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


Written By
Product Manager
Germany Germany
I have been programming (as a hobby) for 20+ years (Unix C, Scripting, VB, C/C++, C#). I am getting too old to talk about it and been in the Security line of work (both Military/Civilian) for 25+ years.

Comments and Discussions

 
Praisevery useful Pin
Southmountain1-Nov-21 10:02
Southmountain1-Nov-21 10:02 
QuestionProblem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 5:32
l_d_allan28-Feb-06 5:32 
AnswerRe: Problem with space 0x20 as delimiter? Pin
Dan Madden28-Feb-06 8:43
Dan Madden28-Feb-06 8:43 
GeneralRe: Problem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 9:22
l_d_allan28-Feb-06 9:22 
Hi Dan,

Thanks for the prompt and helpful reply to my possibly uninformed question. I was pleasantly surprised to get a response to a CodeProject article from 2000.

I hope to avoid "re-inventing the wheel", and I suppose I don't understand how your class is supposed to work. In the test case with delimiters of space, comma, and period, I would have expected the output to be:
one
two
three

I don't understand why "one, two " wasn't split into two tokens.???

I've been working on a specialized tokenizer for searching that only allows A-Z and a-z. The delimiters are anything else. Here are some of the test tokens I use:
char* testStringsWithTokens[] = {
// Test string               Expected results
"",                          "0 ", 
"a",                         "1 <1 a>",
"a b c",                     "3 <1 a><1 b><1 c>",
" a b c ",                   "3 <1 a><1 b><1 c>",
"one two three",             "3 <3 one><3 two><5 three>",
"one\ttwo\tthree",           "3 <3 one><3 two><5 three>",
"one,two,,,, ,,, three,,,",  "3 <3 one><3 two><5 three>",
" one two three",            "3 <3 one><3 two><5 three>",
" one\ttwo\tthree",          "3 <3 one><3 two><5 three>",
" one,two,,,, ,,, three,,,", "3 <3 one><3 two><5 three>",
// snip ... lots more test cases
};
The "even" strings (i.e. 0, 2, 4, etc) are the tests, and the "odd" strings (1, 3, 5, etc) are the cppunit-like expected results (number of tokens and then length of each token found).

Using your code, I would declare the delimiters to be everything except A-Z and a-z.

Is there a way to use your class to accomplish the above? Or am I doing something incorrectly? I really hope to be able to reuse your class in my application.

Thanks again.


-- modified at 19:18 Tuesday 28th February, 2006
GeneralRe: Problem with space 0x20 as delimiter? Pin
Dan Madden28-Feb-06 10:38
Dan Madden28-Feb-06 10:38 
GeneralRe: Problem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 12:07
l_d_allan28-Feb-06 12:07 
GeneralRe: Problem with space 0x20 as delimiter? Pin
Dan Madden2-Mar-06 6:49
Dan Madden2-Mar-06 6:49 
GeneralRe: Problem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 12:16
l_d_allan28-Feb-06 12:16 
GeneralRe: Problem with space 0x20 as delimiter? Pin
Dan Madden2-Mar-06 6:43
Dan Madden2-Mar-06 6:43 
AnswerNew Split Function for this Suggestion Pin
Dan Madden2-Mar-06 7:06
Dan Madden2-Mar-06 7:06 
GeneralRe: New Split Function for this Suggestion Pin
l_d_allan2-Mar-06 16:21
l_d_allan2-Mar-06 16:21 
GeneralProblem with your class Pin
28-Jun-01 10:24
suss28-Jun-01 10:24 
GeneralRe: Problem with your class Pin
Dan Madden28-Jun-01 18:01
Dan Madden28-Jun-01 18:01 
GeneralSTL string tokenizer Pin
Member 112727-Jan-00 2:03
Member 112727-Jan-00 2:03 
GeneralRe: STL string tokenizer Pin
daniel madden30-Jan-00 21:40
daniel madden30-Jan-00 21:40 

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.