Click here to Skip to main content
Click here to Skip to main content

String Tokenizer Class in C++

By , 2 Nov 2001
 

Sample Image - String_Tokenizer_Class_in_C__.gif

Introduction

This class helps the user to tokenize the Long string by specifying delimiter. I thought this will be helpful to the programmers who are at beginning stage.

Code Listing

// Constructor that takes 2 arguments 
// first argument is of string type that to be tokenized. 
// second argument is of string type that is used as token separator 
// and default separator is space 
StringTokenizer::StringTokenizer(CString str,CString sep=" ") 
{ 
    index=0; 
    count=0; 
    CString str1=""; 
    for (int i=0;i<str.GetLength() && sep.GetLength()==1;i++) 
    { 
        if(str.GetAt(i)==sep.GetAt(0)) 
        { 
            elements.Add(str1); 
            str1=""; 
        } 
        else 
        { 
            str1+=str.GetAt(i); 
        } 
    } 
    elements.Add(str1); 
    count=elements.GetSize (); 
} 
// Method is used to fetch the tokens. 
CString StringTokenizer::getNextElement() 
{ 
    index++; 
    if(index==count) 
    { 
        throw CString("Index out of Bounds"); 
    } 
    return elements.GetAt(index-1); 
} 
//method used to fetch the count of tokens from the string 
int StringTokenizer::countElements() 
{ 
    return count; 
} 

//fetch the elements at given position 
CString StringTokenizer::elementAt(int index) 
{ 
    if(index>=count ||index<0) 
    { 
        throw CString("Index out of Bounds"); 
    } 
    else 
        return elements.GetAt(index); 
} 

License

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

About the Author

Koundinya
Web Developer
India India
Member
Hi
I am from South India (Hyderabad). Basically I am lover of mathematics which made me to enter computer field. I started learning computer sciences with language B.B.C Basic.
Later I changed my track to C, C++ and Java.
 

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1membersynne21 Jan '09 - 19:58 
too little info
Generalprecompiled header directivememberbendigi15 Oct '04 - 9:43 
Hi,
 
I get the following error:
\stringtokenizer.cpp(56) : fatal error C1010: unexpected end of file while looking for precompiled header directive
 
and it's pointing to the last line of the cpp file.
 
Any solutions ? Blush | :O
 
.::bendigi::.
GeneralRe: precompiled header directivememberKoundinya11 Dec '04 - 11:14 
simple answer is
 
put #include "stdafx.h" in stringtokenizer.cpp
 
Regards
Sudhakar
Generalstl simplificationmemberTemplMetaProg3 Jul '02 - 11:03 
why not just use istream_iterator?
GeneralRe: stl simplificationsussBonio17 May '04 - 7:58 
Hi,
could you demonstrate how to do it?
Thanks in advance,
Bonio
GeneralRe: stl simplificationmemberKoundinya11 Dec '04 - 11:15 
See this
 
http://www.codeproject.com/string/ctokenex.asp?df=100&forumid=267&select=172#xx172xx

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 3 Nov 2001
Article Copyright 2001 by Koundinya
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid