Original Post
🔒 Locked
Exploding a string
Advertisement
Quote:As far as I know there's no direct C++ equivalent, although Boost.Tokenizer comes close to providing the same functionality. You could also have a look at strtok(), but since you're programming in C++ I would instead recommend taking the Boost route, or writing your own tokenizer using std::string and perhaps other elements from the C++ standard library.
Original post by Drezard
Hello, I was wondering in PHP there is a function called explode();
It explodes a string.
So basicly u pass it a variable containing a string and it splits it and makes it into smaller variable.
Whats the C++ code for this?
- Cheers, Daniel
Note that boost::tokenizer and strtok() are iterative solutions; you'll have to wrap them in your own function to mimic the 'return an array' behavior of explode().
[Edited by - jyk on January 4, 2007 8:11:14 PM]
Here's a quick example of how you might imlement the explode() function using the C++ standard library and Boost.Tokenizer:
std::vector<std::string> explode(const std::string& text, const char* delimiters){ boost::tokenizer< boost::char_separator<char> > tokenizer( text, boost::char_separator<char>(delimiters) ); std::vector<std::string> tokens; std::copy(tokenizer.begin(), tokenizer.end(), std::back_inserter(tokens)); return tokens;}// ...std::vector<std::string> tokens = explode( "The quick, brown fox - jumped over the; lazy dog", " ,-;");Of course if efficiency is a priority you might choose not to return the output vector by value.
You could also just use the Boost String Algorithms Library.
string str1("hello abc-*-ABC-*-aBc goodbye");typedef vector< string > split_vector_type; split_vector_type SplitVec; // #2: Search for tokenssplit( SplitVec, str1, is_any_of("-*") ); // SplitVec == { "hello abc","ABC","aBc goodbye" }
Quote:Yup, that's even more concise than the tokenizer example :)
Original post by mfawcett
You could also just use the Boost String Algorithms Library.string str1("hello abc-*-ABC-*-aBc goodbye");typedef vector< string > split_vector_type; split_vector_type SplitVec; // #2: Search for tokenssplit( SplitVec, str1, is_any_of("-*") ); // SplitVec == { "hello abc","ABC","aBc goodbye" }
I wrote own tokenizer class, which behaves like strtok() in C standard library. It does not split the input string at once, but it returns each tokens when requested. So I can switch the delimiters in the middle of the string. The default delimiters are " ", "\t", "\v", and "\n".
Here is the source code: tokenizer.zip
The useage is:
Here is the source code: tokenizer.zip
The useage is:
...Tokenizer tokenizer;// set input stringtokenizer.set("This is a string.");// get the first token// If it reaches the end of string, it returns a zero-length string, ""std::string token;token = tokenizer.next();...Quote:I'll just point out that based on your description, your Tokenizer class simply mirrors the functionality of boost::tokenizer, except with less flexibility and without being compatible with the standard library (in terms of being iterator-based). This isn't a criticism (I imagine your class was written either before boost::tokenizer became available, or just without being aware of it), but if the OP is interested in a C++ solution I would recommend using boost::tokenizer (or the Boost string algorithms library) instead.
Original post by songho
I wrote own tokenizer class, which behaves like strtok() in C standard library. It does not split the input string at once, but it returns each tokens when requested. So I can switch the delimiters in the middle of the string. The default delimiters are " ", "\t", "\v", and "\n".
Here is the source code: tokenizer.zip
The useage is:
*** Source Snippet Removed ***
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement