string replace

原文地址:http://www.java2s.com/Code/Cpp/Data-Type/StringFindandreplace.htm

#include <string> 
#include <iostream>

using namespace std;

int main()
{
    string str"a bc abc abcd abcde" );
    string searchString"hello" )
    string replaceString"ab" );

    assertsearchString != replaceString );

    string::size_type pos = 0;
    while ( (pos = str.find(searchString, pos)) != string::npos ) {
        str.replacepos, searchString.size(), replaceString );
        pos++;
    }
    cout << str << endl;
    return 0;
}

posted @ 2011-09-04 20:21  wangkangluo1  阅读(245)  评论(0编辑  收藏  举报