From a9f9e6d6eec6a42921e4446baef92eb5b2356d60 Mon Sep 17 00:00:00 2001 From: fluxgen Date: Thu, 20 Oct 2005 14:48:53 +0000 Subject: added replaceString --- src/FbTk/StringUtil.cc | 26 +++++++++++++++++++++++++- src/FbTk/StringUtil.hh | 5 +++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/FbTk/StringUtil.cc b/src/FbTk/StringUtil.cc index c3580ec..3066615 100644 --- a/src/FbTk/StringUtil.cc +++ b/src/FbTk/StringUtil.cc @@ -23,7 +23,7 @@ #include "StringUtil.hh" -#include + #ifdef HAVE_CSTDIO #include #else @@ -44,8 +44,11 @@ #else #include #endif + + #include #include +#include using namespace std; @@ -116,6 +119,27 @@ string findExtension(const std::string &filename) { return filename.substr(start_pos + 1); } +string replaceString(const std::string &original, + const char *findthis, + const char *replace) { + int i=0; + const int size_of_replace = strlen(replace); + const int size_of_find = strlen(findthis); + string ret_str(original); + while (i < ret_str.size()) { + i = ret_str.find(findthis, i); + if (i == std::string::npos) + break; + // erase old string and insert replacement + ret_str.erase(i, size_of_find); + ret_str.insert(i, replace); + // jump to next position after insert + i += size_of_replace; + } + + return ret_str; +} + /** Parses a string between "first" and "last" characters and ignoring ok_chars as whitespaces. The value is diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh index 3112a9d..b79b46a 100644 --- a/src/FbTk/StringUtil.hh +++ b/src/FbTk/StringUtil.hh @@ -41,6 +41,11 @@ std::string expandFilename(const std::string &filename); /// @return extension of filename (ex: filename.txt will return txt) std::string findExtension(const std::string &filename); +/// @return copy of original with find_string replaced with "replace" +std::string replaceString(const std::string &original, + const char *find_string, + const char *replace); + /// returns string between character first and last int getStringBetween(std::string& out, const char *instr, char first, char last, -- cgit v0.11.2