From 1bb6eadceb88636cf0b7433286d35621d500bd8b Mon Sep 17 00:00:00 2001 From: David Sarrut Date: Fri, 3 Feb 2012 07:57:12 +0100 Subject: [PATCH] Add findAndReplace --- common/clitkCommon.h | 5 +++++ common/clitkCommon.txx | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) 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 */ -- 2.45.2