strtok equiavalent in C++ ?

T

tvn007

// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:


ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

// Thanks in advance for any hint,
 
M

mlimber

// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:


ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

// Thanks in advance for any hint,

Check out the Boost Tokenizer library
(http://www.boost.org/libs/tokenizer/).

Cheers! --M
 
N

Neil Cerutti

// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:


ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

The std::string member function find_first_of can do the job of
strtok.

std::string::size_type i = 0;
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
 
N

Neil Cerutti

The std::string member function find_first_of can do the job of
strtok.

std::string::size_type i = 0;
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);
i = testbuff.find_first_of(" \t\n,()", i);

Oops. Sorry. That was utter rubbish.
 
A

Alex Vinokur

// I am using strtok to break the string
// For example: to extract 5 from the string below:
// TEST 1,P,5,PASS
// Below is my code in C:


ptr =strtok(testbuff," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");
ptr =strtok(NULL," \t\n,()");

// My question: is there a better way to do this in C++,
// assumed testbuff is C++ string type.

// Thanks in advance for any hint,


Look at sample "Splitting string into vector of vectors" at
* http://groups.google.com/group/sources/msg/77993fb8841382c8
* http://groups.google.com/group/log-files/msg/305be21f949acdcc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top