您的位置:首页 > 编程语言 > C语言/C++

C++ 字符串应用--一次性替换所有子字符串

2012-09-06 21:17 176 查看
// StringTest.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

//#include <cstddef>

#include "string"

#include "iostream";

using namespace std;

string& ReplaceAll(string& context,const string &from,const string& to)

{

size_t lookHere=0;

size_t foundHere;

while((foundHere=context.find(from,lookHere))!=string::npos)

{

context.replace(foundHere,from.size(),to);

lookHere=foundHere+to.size();

}

return context;

}

int _tmain(int argc, _TCHAR* argv[])

{

//tets

string test="my name is /,/ is a good sutdent";

cout<<ReplaceAll(test,"/","justin")<<endl;

int k=0;

cin>>k;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: