added replaceString
This commit is contained in:
parent
4dec832b6b
commit
a9f9e6d6ee
2 changed files with 30 additions and 1 deletions
|
@ -23,7 +23,7 @@
|
|||
|
||||
#include "StringUtil.hh"
|
||||
|
||||
#include <string>
|
||||
|
||||
#ifdef HAVE_CSTDIO
|
||||
#include <cstdio>
|
||||
#else
|
||||
|
@ -44,8 +44,11 @@
|
|||
#else
|
||||
#include <assert.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue