By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

strtok equiavalent in C++ ?

P: n/a

// 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,

Oct 24 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
tv****@hotmail.com wrote:
// 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

Oct 24 '05 #2

P: n/a
On 2005-10-24, tv****@hotmail.com <tv****@hotmail.com> wrote:

// 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);

--
Neil Cerutti
Oct 24 '05 #3

P: n/a
On 2005-10-24, Neil Cerutti <le*******@email.com> wrote:
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.

--
Neil Cerutti
Oct 24 '05 #4

P: n/a

<tv****@hotmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...

// 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/sourc...993fb8841382c8
* http://groups.google.com/group/log-f...5be21f949acdcc

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Oct 25 '05 #5

P: n/a
On 2005-10-25, Alex Vinokur <al****@go.to> wrote:

<tv****@hotmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...

// 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/sourc...993fb8841382c8
* http://groups.google.com/group/log-f...5be21f949acdcc


In addition, a chapter of _Accelerated C++_ (Koenig, Moo) is
devoted to developing a standard algorithm style template
function for splitting sequences into subsequences.

--
Neil Cerutti
Oct 25 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.