# include <String.h> ... class StringScan { public: enum ScanType { ByField,ByMatch }; StringScan(String &s, ScanType t, const StringSearch &s_s); int operator()(String &match); int cursor(); private: String *str; const StringSearch *ss; ScanType st; StringRange pos; char lf; }; ...
ByField scans through a String using the given field as a seperator. In this respect it is like the split funtion.
ByMatch scans through a String and returns successive matches using the
specified StringSearch function.
The String being scanned should not be changed during the scan. If the String is to be changed, then Scan should be passed a copy of the String.
StringScan Word(s1,StringScan::ByMatch,RXnonwhite); Word(w); Assert(w=="This"); Word(w); Assert(w=="is"); Word(w); Assert(w=="a"); Word(w); Assert(w=="test");
String s2("schemers:*:271:79:Roland Schemers:/u/schemers:/bin/csh");
StringScan next_field(s2,StringScan::ByField,String(":")); String pf;
next_field(pf); Assert(pf=="schemers"); next_field(pf); Assert(pf=="*"); next_field(pf); Assert(pf=="271"); next_field(pf); Assert(pf=="79"); next_field(pf); Assert(pf=="Roland Schemers"); next_field(pf); Assert(pf=="/u/schemers"); next_field(pf); Assert(pf=="/bin/csh");
s2="one 1 two 2 three3four 4 negative five -5"; StringScan next_num(s2,StringScan::ByMatch,RXint); String num;
next_num(num); Assert(num=="1"); next_num(num); Assert(num=="2"); next_num(num); Assert(num=="3"); next_num(num); Assert(num=="4"); next_num(num); Assert(num=="-5");