Click here to Skip to main content
11,805,035 members (46,170 online)
Click here to Skip to main content

String Tokenizer Class in C++

, 2 Nov 2001 CPOL 81.6K 1.1K 24
Rate this:
Please Sign up or sign in to vote.
As C++ doesn't have Java Equivalent StringTokenizer class, I have implemented the class for my own and helps the beginners

Sample Image - String_Tokenizer_Class_in_C__.gif


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=" ") 
    CString str1=""; 
    for (int i=0;i<str.GetLength() && sep.GetLength()==1;i++) 
    count=elements.GetSize (); 
// Method is used to fetch the tokens. 
CString StringTokenizer::getNextElement() 
        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"); 
        return elements.GetAt(index); 


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


About the Author

Web Developer
India India
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.

You may also be interested in...

Comments and Discussions

SuggestionSimpler way using CString::Tokenize Pin
Elimad3-Oct-14 20:56
memberElimad3-Oct-14 20:56 
GeneralMy vote of 3 Pin
Manikandan106-Jun-14 20:58
professionalManikandan106-Jun-14 20:58 
GeneralMy vote of 1 Pin
synne21-Jan-09 19:58
membersynne21-Jan-09 19:58 
Generalprecompiled header directive Pin
bendigi15-Oct-04 9:43
memberbendigi15-Oct-04 9:43 
GeneralRe: precompiled header directive Pin
Koundinya11-Dec-04 11:14
memberKoundinya11-Dec-04 11:14 
Generalstl simplification Pin
TemplMetaProg3-Jul-02 11:03
memberTemplMetaProg3-Jul-02 11:03 
GeneralRe: stl simplification Pin
Bonio17-May-04 7:58
sussBonio17-May-04 7:58 
GeneralRe: stl simplification Pin
Koundinya11-Dec-04 11:15
memberKoundinya11-Dec-04 11:15 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 3 Nov 2001
Article Copyright 2001 by Koundinya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid