From: David Sarrut Date: Fri, 3 Feb 2012 06:57:12 +0000 (+0100) Subject: Add findAndReplace X-Git-Tag: v1.4.0~253 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=1bb6eadceb88636cf0b7433286d35621d500bd8b;hp=e64bc938065fef91ca19fb4b5f4f3bb973bc6f81;p=clitk.git Add findAndReplace --- diff --git a/common/clitkCommon.h b/common/clitkCommon.h index 8f9ce0f..8cd82fb 100644 --- a/common/clitkCommon.h +++ b/common/clitkCommon.h @@ -235,6 +235,11 @@ namespace clitk { template void MapToVecSecond(const M & m, V & v); + //-------------------------------------------------------------------- + // Find/replace string + template + int inline findAndReplace(T& source, const T& find, const T& replace); + #include "clitkCommon.txx" } // end namespace diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index 5ee83bd..845e62f 100644 --- a/common/clitkCommon.txx +++ b/common/clitkCommon.txx @@ -235,5 +235,22 @@ void MapToVecSecond(const M & m, V & v) { } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +//http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string +template +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; +} +//-------------------------------------------------------------------- + #endif /* end #define CLITKCOMMON_TXX */