 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
PengYu.UT@gmail.com Guest
|
Posted: Tue Nov 29, 2005 11:43 pm Post subject: how to split a string in C++? |
|
|
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
|
Posted: Tue Nov 29, 2005 11:51 pm Post subject: Re: how to split a string in C++? |
|
|
[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
|
Posted: Wed Nov 30, 2005 12:25 am Post subject: [OT] Re: how to split a string in C++? |
|
|
[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
|
Posted: Wed Nov 30, 2005 12:35 am Post subject: Re: how to split a string in C++? |
|
|
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
|
Posted: Wed Nov 30, 2005 1:39 am Post subject: Re: how to split a string in C++? |
|
|
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
|
Posted: Wed Nov 30, 2005 2:46 am Post subject: Re: how to split a string in C++? |
|
|
try www.boost.org
(yes, you'll need two brains at least)
|
|
| Back to top |
|
 |
|
|
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
|
|