C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to split a string in C++?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
PengYu.UT@gmail.com
Guest





PostPosted: Tue Nov 29, 2005 11:43 pm    Post subject: how to split a string in C++? Reply with quote



In perl, I can do the following thing to split a string.

my ($benchmark, $suffix) = split(/./, $fullbenchmarks);

I'm wondering if there is similar thing in C++ which can do the same
thing. Would you please tell me if you know it?

Thanks,
Peng

Back to top
Victor Bazarov
Guest





PostPosted: Tue Nov 29, 2005 11:51 pm    Post subject: Re: how to split a string in C++? Reply with quote



[email]PengYu.UT (AT) gmail (DOT) com[/email] wrote:
Quote:
In perl, I can do the following thing to split a string.

my ($benchmark, $suffix) = split(/./, $fullbenchmarks);

I'm wondering if there is similar thing in C++ which can do the same
thing. Would you please tell me if you know it?

There is no such thing yet. You need to roll your own. Use 'substr'
and 'find' member functions of the string class.

V

Back to top
Larry I Smith
Guest





PostPosted: Wed Nov 30, 2005 12:25 am    Post subject: [OT] Re: how to split a string in C++? Reply with quote



[email]PengYu.UT (AT) gmail (DOT) com[/email] wrote:
Quote:
In perl, I can do the following thing to split a string.

my ($benchmark, $suffix) = split(/./, $fullbenchmarks);

I'm wondering if there is similar thing in C++ which can do the same
thing. Would you please tell me if you know it?

Thanks,
Peng


Depending on your platform, the POSIX Regular Expression
library may be available for use in your C/C++ code.
On unix/linux, if 'man regexec' displays the manual pages,
then the library is installed.

Larry

Back to top
Default User
Guest





PostPosted: Wed Nov 30, 2005 12:35 am    Post subject: Re: how to split a string in C++? Reply with quote

Victor Bazarov wrote:

Quote:
PengYu.UT (AT) gmail (DOT) com wrote:
In perl, I can do the following thing to split a string.

my ($benchmark, $suffix) = split(/./, $fullbenchmarks);

I'm wondering if there is similar thing in C++ which can do the same
thing. Would you please tell me if you know it?

There is no such thing yet. You need to roll your own. Use 'substr'
and 'find' member functions of the string class.


Or search Google groups in this group for previously posted examples,
those cover the basics. The perl split() has more power than the
somewhat simplistic ones posted here as far as I recall, but they'd
provide a good starting point.



Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

Back to top
pepp
Guest





PostPosted: Wed Nov 30, 2005 1:39 am    Post subject: Re: how to split a string in C++? Reply with quote

A partial solution would be:

inline bool space(char c){
return std::isspace(c);
}

inline bool notspace(char c){
return !std::isspace(c);
}



//break a sentence into words
std::vector<std::string> split(const std::string& s){
typedef std::string::const_iterator iter;
std::vector<std::string> ret;
iter i = s.begin();
while(i!=s.end()){
i = std::find_if(i, s.end(), notspace); // find the beginning of a
word
iter j= std::find_if(i, s.end(), space); // find the end of the same
word
if(i!=s.end()){
ret.push_back(std::string(i, j)); //insert the word into vector
i = j; // repeat 1,2,3 on the rest of the line.
}

}
return ret;
}

Back to top
beryan
Guest





PostPosted: Wed Nov 30, 2005 2:46 am    Post subject: Re: how to split a string in C++? Reply with quote

try www.boost.org
(yes, you'll need two brains at least)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.