![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
"split" function
Is there a C++ or C standard library function that splits an std::string (or char*) into words, based on a certain delimiter? What I'm looking for is something like this:
at the beginning, line == "the big blue dog ate food." words is an array of std::strings whatever_function(line,words,' '); at the end, words should equal {"the","big","blue","dog","ate","food"}
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
strtok can work for you.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#3 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4
![]() |
int main(int argc, char* argv[])
{
string str = "the big blue dog ate food.";
int pos;
while((pos = str.find(' ')) > 0)
{
string sub = str.substr(0,pos);
str = str.substr(pos+1);
cout << sub << endl;
}
return 0;
} |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
No, not to my knowledge. Unfortunately strtok only works with single character delimiters and it takes a const char pointer argument instead of a std::string. The less const char pointers I see in my code, the happier I tend to be
. You can also use std::getline to do this, but again that takes a single char as a delimiter. Here's the simple function I wrote to do this. // Returns a std::vector of the split strings
#include <vector>
#include <string>
std::vector<std::string> split(std::string str, std::string delimiter) {
int i = 0;
std::vector<std::string> stringvector;
while(str.find(delimiter, i) != std::string::npos) {
i = str.find(delimiter, i);
stringvector.push_back(str.substr(0, i));
str = str.substr(i + delimiter.size(), str.length() - (i + delimiter.size()));
i += delimiter.size();
}
return stringvector;
}int main() {
std::string s = "This|||is|||fun|||isn't|||it";
std::cout << split(s, "|||")[0] << "\n"; // prints 'This'
return 0;
} |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Strtok takes multiple delimiters, but it is intended for use with arrays of char, not std strings. Furthermore, it alters the original, so even where one can use it, one may need to copy the original first. When PHP nabbed it as a utility, they burned off some of the warts.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
ok
getline + istringstream seems to be what I'm looking for. Thanks all!
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#7 |
|
Programmer
|
I would say boost/algorithm/string/split.hpp is what u need. its still no standart functions , but a lot of the boost libs will become standart in the 0x10 c++ standart and i m pretty sure that the string-algo lib will...
btw boost is so powerfull u should have a look or even learn to handle some parts. like boost::split the function gets a sequence,a collection and a prdicate, and an so called token compress mode type, but u dont need that one. so for your purposes it would look like this template<typename SequenceSequenceT, typename CollectionT,
typename PredicateT>
SequenceSequenceT &
split(SequenceSequenceT &, CollectionT &, PredicateT,
token_compress_mode_type = token_compress_off);split(SequenceSequenceT &, CollectionT &, PredicateT); id just wrote this little testmain. i think its exactly what you ask for: #include<iostream>
#include<string>
#include<vector>
#include<boost/algorithm/string.hpp>
#include<algorithm>//test output
#include<iterator>// test output
int main() {
std::string line("hallo-this-is-a-string");
std::vector<std::string> result;
boost::split(result,line,boost::is_any_of("-"));
// puts the result of a split of line into the
// result container without things that match to the prediate
//"-" or in your case " "
if(!result.empty())
std::copy(result.begin(),result.end(),std::ostream_iterator<std::string>(std::cout,"\n"));
return 0;
}your wish: function(container,string,delim); solution: split(container,string,predicate); where predicate is the delim... can use it on every containers convertable to each other.
__________________
of all the things he has lost, i think he misses his mind the most typedef typename pizza_t<oven_policy<225,12.5>,ingredient_policy<salami,mushroom,cheese> > Pizza; |
|
|
|
|
|
#8 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#9 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 489
Rep Power: 4
![]() |
The problem posted by OP is so simple -- Forget about using all those other complex templates that do nothing more than make the solution to the problem more complicated. You guys are making such a complicated solution out of something that is very trivel. :eek:
Last edited by Ancient Dragon; Jul 23rd, 2005 at 9:39 PM. |
|
|
|
|
|
#10 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|