Click here to Skip to main content
14,769,372 members
Articles » General Programming » String handling » General
Article
Posted 26 Jan 2000

Stats

101.3K views
2.3K downloads
29 bookmarked

String Tokenizer Class (CTokenEx)

Rate me:
Please Sign up or sign in to vote.
4.61/5 (16 votes)
26 Jan 2000CPOL
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)

Share

About the Author

Dan Madden
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

 
QuestionProblem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 6:32
Memberl_d_allan28-Feb-06 6:32 
AnswerRe: Problem with space 0x20 as delimiter? Pin
Dan Madden28-Feb-06 9:43
MemberDan Madden28-Feb-06 9:43 
GeneralRe: Problem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 10:22
Memberl_d_allan28-Feb-06 10:22 
GeneralRe: Problem with space 0x20 as delimiter? Pin
Dan Madden28-Feb-06 11:38
MemberDan Madden28-Feb-06 11:38 
GeneralRe: Problem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 13:07
Memberl_d_allan28-Feb-06 13:07 
Hi Dan,

Thanks ... I think that will make it work both "my way" and "your way". We apparently have different notions of what a delimiter is, and how to handle it.

Actually, I don't understand why the original/default is available. I would think you would ALWAYS want to "throw away" any delimiter and use that to break up tokens, whether the delimiters show up in multiples or not.

I am perhaps being slow to understand why you would want to specify bMultipleDelimter in any way other than causing "one, two ,. three" to come out:
"one"
"two"
"three"

Not meaning to be argumentative, but I would think the expected behavior would pretty much ALWAYS be that "one, two ,. three" would be handled that way, and I would think the bMultipleDelimiter would be left out of the parameter list. To me, it detracts and creates potential confusion to have that option (but perhaps I am being slow ... I realize that I can be "not the brightest bulb in the box". It has been a long day.<g>

And again thanks for providing the code and helping out this confused person.
GeneralRe: Problem with space 0x20 as delimiter? Pin
Dan Madden2-Mar-06 7:49
MemberDan Madden2-Mar-06 7:49 
GeneralRe: Problem with space 0x20 as delimiter? Pin
l_d_allan28-Feb-06 13:16
Memberl_d_allan28-Feb-06 13:16 
GeneralRe: Problem with space 0x20 as delimiter? Pin
Dan Madden2-Mar-06 7:43
MemberDan Madden2-Mar-06 7:43 
AnswerNew Split Function for this Suggestion Pin
Dan Madden2-Mar-06 8:06
MemberDan Madden2-Mar-06 8:06 
GeneralRe: New Split Function for this Suggestion Pin
l_d_allan2-Mar-06 17:21
Memberl_d_allan2-Mar-06 17:21 
GeneralProblem with your class Pin
28-Jun-01 11:24
suss28-Jun-01 11:24 
GeneralRe: Problem with your class Pin
Dan Madden28-Jun-01 19:01
MemberDan Madden28-Jun-01 19:01 
GeneralSTL string tokenizer Pin
Member 112727-Jan-00 3:03
MemberMember 112727-Jan-00 3:03 
GeneralRe: STL string tokenizer Pin
daniel madden30-Jan-00 22:40
Memberdaniel madden30-Jan-00 22: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.