Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2004-09-08 02:41:02


On Wed, Sep 08, 2004 at 10:59:32AM +0400, Vladimir Prus wrote:
> Hello,
> suppose I have a string "module.foo1.port" and want to get the second
> dot-separated element. I think I can use the 'split' algorithm, but it does
> not look very convenient, I need to declare container, then call split and
> then obtain the result.

You can also use split_iterator.

#include <boost/algorithm/string/find_iterator.hpp>
#include <boost/algorithm/string/finder.hpp>

using namespace boost;

std::string str="module.foo1.port";
typedef split_iterator<std::string::iterator> string_split;

string_split it(str, token_finder(is_any_of(".")); // *it="module"
++it; // *it="foo1"
++it; // *it="port"
++it; // it.eof()==true it=string_split();

*it is an iterator_range, pointing to the input. You can easily convert it to a string

std::string match=copy_iterator_range<std::string>(*it);

> In Qt, there's QString::section method, which allows to do this in one line.
> Here's example from the docs:
> QString csv( "forename,middlename,surname,phone" );
> QString s = csv.section( ',', 2, 2 ); // s == "surname"
> And the complete docs are at:
> Maybe, something like this can be added?

Seem useful. I will see, how it can be added. Thanks for an idea.



Boost list run by bdawes at, gregod at, cpdaniel at, john at