String Tokenizer Class (CTokenEx)






4.66/5 (17 votes)
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 aCString
from aCStringArray
with delimiters of your choice. - use
Split
to break-up aCString
into aCStringArray
(according to the delimiter). - use
GetString
to get the first sub-string in aCString
(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.