The basic_substring class provides generic substring capabilities to the standard string class, as recommended in Section 20.3.13 in "The C++ Programming Language" by Stroustrup, B. But, not being content to just duplicate what I saw there, I had to extend it a little bit.
Use the constructors to create a new substring. If you add a 'true' to the list of parameters, the substring will work in reverse (starting at the end, and working its way towards the beginning).
You may iterate to the next matching substring using either of the iterator operators (post or prefix, decrement or increment). The iterators will not modify the actual string.
The only way to actually modify a string through the substring is to assign a string to the substring. This causes the substring to replace the substring in the string with whatever you assigned, and iterate the substring to the next matching substring.
When you iterate past the end, the substring becomes invalid, and is no longer useful. You may, however, assign another substring to it (assigning substrings to a substring acts as a copy, and does not modify the string).
You may use the string operator to acquire the text of the substring.
You may also use the bool operator to test whether or not you have a valid substring.
The included test program also acts as a set of examples.
Feel free to use this without restriction. This is donated to the public domain.
basic_substring.tar.bz2 (29424 bytes)