472,962 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Help with Tokenizer Please

I use a code snippet called tokenize that works quite well. But I need
to modify it so it returns an empty string if two commas are found
together: ie. string test="1,2,,4,5"

I would like to have

tokens[0] = "1"
tokens[1] = "2"
tokens[3] = ""
tokens[4] = "4"
tokens[5] = "5"

This would allow me to find missing tokens. The following is the code I
use.
Can anyone show me the modification I would need?
-----------------

void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " ,\t\n")
{
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Skip delimiters at beginning.
string::size_type pos = str.find_first_of(delimiters, lastPos);
// Find first "non-delimiter".
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Found a token, add it to the vector.
lastPos = str.find_first_not_of(delimiters, pos);
// Skip delimiters. Note the "not_of"
pos = str.find_first_of(delimiters, lastPos);
// Find next "non-delimiter"
}
}

--------------

Thanks in advance,
Grant

Feb 26 '06 #1
1 1566
electrixnow wrote:
I use a code snippet called tokenize that works quite well. But I need
to modify it so it returns an empty string if two commas are found
together: ie. string test="1,2,,4,5"

I would like to have

tokens[0] = "1"
tokens[1] = "2"
tokens[3] = ""
tokens[4] = "4"
tokens[5] = "5"

This would allow me to find missing tokens. The following is the code
I use.
Can anyone show me the modification I would need?
-----------------

void Tokenize(const string& str,
vector<string>& tokens,
const string& delimiters = " ,\t\n")
{
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Skip delimiters at beginning.
string::size_type pos = str.find_first_of(delimiters, lastPos);
// Find first "non-delimiter".
while (string::npos != pos || string::npos != lastPos)
{
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Found a token, add it to the vector.
lastPos = str.find_first_not_of(delimiters, pos);
// Skip delimiters. Note the "not_of"
I think you shouldn't skip delimiters here. You just need to make
'lastPos' to be the same as 'pos'. Replace the statement above with

lastPos = pos;
pos = str.find_first_of(delimiters, lastPos);
// Find next "non-delimiter"
}
}

--------------


I didn't check it. It's just a hunch.

V
--
Please remove capital As from my address when replying by mail
Feb 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Knackeback | last post by:
task: - read/parse CSV file code snippet: string key,line; typedef tokenizer<char_separator<char> > tokenizer; tokenizer tok(string(""), sep); while ( getline(f, line) ){ ++lineNo;...
4
by: Java Guy | last post by:
This must be a classical topic -- C++ stgring tokenizer. I just switched from C to C++ ( in Unix ). It turns out that there is no existing C++ string tokenizer. Searching on the Web, I found...
10
by: Lorenzo J. Lucchini | last post by:
Do you see any counter-indication to a token extractor in the following form? typedef int token; token ExtractToken(const char** String, char* Symbol); If it finds a valid token at the start...
4
by: mca | last post by:
When I try to read tokens from a text file, c# does not accept "-" (minus sign) as a valid token and tokenizer always throws exception. What should I do to make "-" sign valid Important note :...
1
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
4
by: nnguyec | last post by:
Hi, I'm trying to write a small code for an assignment which the void String Tokenizer will get a line input, and take out those delimiters from the original string. Then pass each string without the...
11
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package...
0
by: madhumv12 | last post by:
hi, please check this code and tell me how can i print the values of tokenizer.getStringAt(0) and (1) and (2). tell me that how can i print individual content of a string? cecTokenizer...
2
by: laksh2007 | last post by:
i want to search and replace strings present in a file (entire file).. i have wirtten cod eto do this.. but it is not working as expected.. code is : import java.io.*; import java.util.*;
1
by: Karl Kobata | last post by:
Hi Fredrik, This is exactly what I need. Thank you. I would like to do one additional function. I am not using the tokenizer to parse python code. It happens to work very well for my...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.