Click here to Skip to main content
Licence CPOL
First Posted 26 Jan 2000
Views 82,775
Downloads 1,638
Bookmarked 24 times

String Tokenizer Class (CTokenEx)

By Dan Madden | 26 Jan 2000
A very simple string tokenizer class.

1

2

3
3 votes, 20.0%
4
12 votes, 80.0%
5
4.65/5 - 26 votes
μ 4.46, σa 0.72 [?]

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)

About the Author

Dan Madden

Engineer

Germany Germany

Member
I have been programming for 19 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.

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
QuestionProblem with space 0x20 as delimiter? Pinmemberl_d_allan6:32 28 Feb '06  
AnswerRe: Problem with space 0x20 as delimiter? PinmemberDan Madden9:43 28 Feb '06  
GeneralRe: Problem with space 0x20 as delimiter? Pinmemberl_d_allan10:22 28 Feb '06  
GeneralRe: Problem with space 0x20 as delimiter? PinmemberDan Madden11:38 28 Feb '06  
GeneralRe: Problem with space 0x20 as delimiter? Pinmemberl_d_allan13:07 28 Feb '06  
GeneralRe: Problem with space 0x20 as delimiter? PinmemberDan Madden7:49 2 Mar '06  
GeneralRe: Problem with space 0x20 as delimiter? Pinmemberl_d_allan13:16 28 Feb '06  
GeneralRe: Problem with space 0x20 as delimiter? PinmemberDan Madden7:43 2 Mar '06  
AnswerNew Split Function for this Suggestion PinmemberDan Madden8:06 2 Mar '06  
Here it is (thanks l_d_allan):
 
By using the code below, you could test it by doing this in a "main()":
CTokenEx      tok;
CString       csSplit("one, two ,. three");
CStringArray  splitIt;
CString       m_deliminator = ",. ";
 
tok.Split(csSplit, m_deliminator, splitIt, FALSE);
 
printf("\n%s\n",csSplit);
 
for (int n=0; n<splitIt.GetSize(); n++)
{
	printf("\"%s\"\n",splitIt.GetAt(n));
}
This would produce:
one, two ,. three
"one"
"two"
"three"
void CTokenEx::Split(CString Source, CString Deliminator, CStringArray& AddIt, BOOL bAddEmpty)
{
	// initialize the variables
	CString		 newCString = Source;
	CString		 tmpCString = "";
	CString		 AddCString = "";
 
	int pos1 = 0;
	int pos = 0;
 
	AddIt.RemoveAll();
 
	if (Deliminator.IsEmpty()) {
		// Add default [comma] if empty!
		// acknowledgement: Prasad [gprasad@rti.ie]
		Deliminator = ","; 
	}
 
	//
	CString csStr = newCString;
	int nDelCount = Deliminator.GetLength();
 
	///////////////////////////////////////////////////
	//
	// The below block was created as a suggestion 
	// from "l_d_allan" at CodeProject.com
	//
	CString csMultDel = _T("");
	for (int n=0; n<nDelCount; n++) 
	{
		csMultDel = _T("");
		csMultDel += Deliminator[n];
		csStr.Replace(csMultDel,"~");
	}
	Deliminator = _T("~");
	newCString = csStr;
	//
	///////////////////////////////////////////////////

 
	// do this loop as long as you have a deliminator
	do {
		// set to zero
		pos1 = 0;
		// position of deliminator starting at pos1 (0)
		pos = newCString.Find(Deliminator, pos1);
		// if the deliminator is found...
		if ( pos != -1 ) {
 
			// load a new var with the info left
			// of the position
			CString AddCString = newCString.Left(pos);
 
			if (!AddCString.IsEmpty()) {
				// if there is a string to add, then
				// add it to the Array
				AddIt.Add(AddCString);
			}
			else if (bAddEmpty) {
				// if empty strings are ok, then add them
				AddIt.Add(AddCString);
			}
 
			// make a copy of the of this var. with the info
			// right of the deliminator
			tmpCString = newCString.Mid(pos + Deliminator.GetLength());
			
			// reset this var with new info
			newCString = tmpCString;
		}
	} while ( pos != -1 );
	
	if ((!newCString.IsEmpty()) || bAddEmpty) {
		// as long as the variable is not emty, add it
		AddIt.Add(newCString);
	}
}

 
Regards,
 
Dan
GeneralRe: New Split Function for this Suggestion Pinmemberl_d_allan17:21 2 Mar '06  
GeneralProblem with your class PinmemberAhmed Reda11:24 28 Jun '01  
GeneralRe: Problem with your class PinmemberDaniel Madden19:01 28 Jun '01  
GeneralSTL string tokenizer PinsussDave Lorde3:03 27 Jan '00  
GeneralRe: STL string tokenizer PinsussDaniel Madden22:40 30 Jan '00  

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
Web01 | 2.5.120210.1 | Last Updated 27 Jan 2000
Article Copyright 2000 by Dan Madden
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid