Find and replace a string in c++
This can be handy many a times when you are working on a C++ application. There is a no direct method in the standard to do the same except when you are using a boost library. Below is a simple function that I use regularly in my applications which comes in handy for me all the timetemplate<class T> int inline findAndReplace(T& source, const T& find, const T& replace) { int num=0; int fLen = find.size(); int rLen = replace.size(); for (int pos=0; (pos=source.find(find, pos))!=T::npos; pos+=rLen) { num++; source.replace(pos, fLen, replace); } return num; }
0 comments: