//: C03:StringReplace.cpp // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Simple find-and-replace in strings. #include #include using namespace std; int main() { string s("A piece of text"); string tag("$tag$"); s.insert(8, tag + ' '); assert(s == "A piece $tag$ of text"); int start = s.find(tag); assert(start == 8); assert(tag.size() == 5); s.replace(start, tag.size(), "hello there"); assert(s == "A piece hello there of text"); } ///:~