This article was contributed by Daniel Madden.
Environment: VC++ 6.0 SP3 (using Warning Level 4 in RELEASE Mode), Environments Tested: WinNT 4.0 SP4, Win95
Basically, I saw other string tokenizers here (CodeGuru) and they lacked the functionality I was looking for. Therefore, I created one for myself using the KISS method. This is a VERY SIMPLE sample!!!!
Here is a summary of the functionality in the CTokenEx class:
Say you have a CString that contains: "abc,def,,,ghi,,jkl,,"
//******************************************************** // Split Function //******************************************************** // // Split will fill an array with: // // String Position // ====== ======== // abc 0 // def 1 // ghi 2 // jkl 3 // //******************************************************** void CTokenDlgDlg::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); } /******************************************************** // GetString Function //******************************************************** // // GetString will return a string: // // abc // ...and more calls to GetString will return a strings: // def // ghi // jkl // //******************************************************** void CTokenDlgDlg::OnGetstring() { CTokenEx tok; char Buf[254]; CString csRef = "abc,def,,,ghi,,jkl,,"; do { CString csRet = tok.GetString(csRef, ","); // Do something with the returned value. } while (!csRef.IsEmpty()); }
I hope that others find this class useful.
Add Comment
INSTANT EXPERTS!Savvy IT Pros report on SupportSource. Take a look inside the largest vault of multi-vendor computing specs ever assembled. Click here for an In-depth Report,. For tons of Windows technology resources, check out the Windows Programming directory. Searching the Web for tech topics just got better withTechCrawler where all results come only from popular technical sites. WINNER!!! Congratulations to James Hole, of New Mexico! The grand prize winner of EarthWeb's Flatten the Obstacles Sweepstakes and now a proud Hummer(R) owner.
WINNER!!! Congratulations to James Hole, of New Mexico! The grand prize winner of EarthWeb's Flatten the Obstacles Sweepstakes and now a proud Hummer(R) owner.