Re: explode equivalent in c++
On Jun 19, 1:35 am, DJH <nos...@nospam.com> wrote:
I am trying to find the most efficient way to code a php explode in C++.
What existing STL function may I use to separate this in to its parts
where the delimiter is "||" ?
example:
~!||1||1||foobar
I would like a string array as such
stringarray[0]="~!"
stringarray[1]="1"
stringarray[2]="1"
stringarray[3]="foobar"
You might want to look at the code for FieldArray and its
subclasses in the Text subsystem at my site
(http://kanze.james.neuf.fr/code-en.html). You'll probably have
to adapt it some; it's old code, and for example,
RegularExpressionSeparatedFields uses my own RegularExpression
class, rather than that of Boost. But it's simple to use:
RegularExpressionSeparatedFields
fields( "\\|\\|" ) ;
std::string line ;
while ( std::getline( in, line ) ) {
fields = line ;
// fields[0] contains the entire line, fields[1] the
// first field, fields[2] the second, etc.
}
The base class FieldArray uses the strategy pattern to decide
how to cut up the string, so it would be easy to add additional
critera. (All RegularExpressionSeparatedFields does in addition
is to provide the delegate.) It should be simple, for example,
to provide a delegate which uses a fixed string as a separator;
just replace std::find in CharacterSeparatedFieldSplitter with
std::search.
--
James Kanze (GABI Software, from CAI) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34