473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

stringTokenizer

Ganon11
3,652 Expert 2GB
Hey,

Last year I was working in Java, and one of the predefined classes we used was the stringTokenizer.java class. I wanted to use this in C++, but couldn't find a predefined class. So I made my own class - here's the code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class stringTokenizer {
  6.       public:
  7.              void setMessage(string newMessage);
  8.              string next();
  9.              bool hasNext();
  10.              stringTokenizer(string myMes, char myCh, bool retDel);
  11.              stringTokenizer(string myMes, char myCh);
  12.              stringTokenizer(string myMes);
  13.  
  14.       private:
  15.               void processBlanks();
  16.               string message;
  17.               char ch;
  18.               bool delRet;
  19. };
  20.  
  21. stringTokenizer::stringTokenizer(string myMes, char myCh, bool retDel) {
  22.     message = myMes;
  23.     ch = myCh;
  24.     delRet = retDel;
  25. }
  26.  
  27. stringTokenizer::stringTokenizer(string myMes, char myCh) {
  28.     message = myMes;
  29.     ch = myCh;
  30.     delRet = false;
  31. }
  32.  
  33. stringTokenizer::stringTokenizer(string myMes) {
  34.     message = myMes;
  35.     ch = ' ';
  36.     delRet = false;
  37. }
  38.  
  39. void stringTokenizer::setMessage(string newMessage) {
  40.      message = newMessage;
  41. }
  42.  
  43. string stringTokenizer::next() {
  44.     string word;
  45.     if (!delRet) {
  46.         processBlanks();
  47.         int pos = message.find(ch);
  48.         if (pos != string::npos) {
  49.             word = message.substr(0, pos);
  50.             message = message.substr(pos);
  51.         } else {
  52.             word = message;
  53.             message = "";
  54.         }
  55.     } else {
  56.         if (message[0] == ch) {
  57.             word = message.substr(0, 1);
  58.             message = message.substr(1);
  59.         } else {
  60.             int pos = message.find(ch);
  61.             if (pos != string::npos) {
  62.                 word = message.substr(0, pos);
  63.                 message = message.substr(pos);
  64.             } else {
  65.                 word = message;
  66.                 message = "";
  67.             }
  68.         }
  69.     }
  70.     return word;
  71. }
  72.  
  73. bool stringTokenizer::hasNext() {
  74.      return message.length() > 0;
  75. }
  76.  
  77. void stringTokenizer::processBlanks() {
  78.      while (message.length() > 0 && message[0] == ch)
  79.            message = message.substr(1);
  80.      return;
  81. }
My question is, for any Java/C++ users out there, is there anything I can improve to make this as identical as possible in implementation as the Java class? I have looked at the Java summary of the stringTokenizer class at http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

Thanks for any help.
Nov 29 '06 #1
1 6598
smithan
16
u can check this website which gives information about string tokenizers in c++

http://www.codeproject.com/cpp/string_tokenizer_class_in_c__.asp

http://www.partow.net/programming/stringtokenizer/index.html

hope its of some use,

thank you,

smitha
Nov 29 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rick | last post by:
Hi, I'm having a little problem with the string tokenizer. For example, I got this data string : "0#0#0#0####0#0#0#.. and so on....." How obvious, the # char will be the delimiter. But as you...
3
by: - Steve - | last post by:
It doesn't appear that Visual Studio 2003 has the StringTokenizer class for a J# project. Am I missing something? Steve
1
by: Jong Choi | last post by:
Dear All, I am trying to open the file(walk5.dat) and then read the data The structures of data are 0, 0, -10, -10, 0, -8.1,-12,-10 , 0,-14,-10,-7,0, 6.096666667,-10,-10,1, ...
5
by: Carlitos | last post by:
Hi there, What would be the counterpart for Java's StringTokenizer class in .NET? In fact, does anybody know a good web site that gives this kind of information, like a Java-.NET/.NET-Java...
2
by: Bilwin | last post by:
Hi friends, How can i convert StringTokenizer to string array? friends please reply me..
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
0
ak1dnar
by: ak1dnar | last post by:
There is a Error getting while i am entering records using this jsp file. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ include...
6
by: js06am | last post by:
The API specification for the Java 2 Platform Standard Edition 5.0 states that the class StringTokenizer is 'a legacy class.' What does this mean? a.m.
1
by: xmra | last post by:
Hi, i'm doing an assigment in java. I have a text file, the format is the following: Massachusettes: 016,017,018,019,020,021,022,023,024,025,026,027,055 Rhode Island: 028,029 New Hampshire:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.