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 "StringUtil.hh"
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#ifdef HAVE_CSTDIO
|
#ifdef HAVE_CSTDIO
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#else
|
#else
|
||||||
|
@ -44,8 +44,11 @@
|
||||||
#else
|
#else
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -116,6 +119,27 @@ string findExtension(const std::string &filename) {
|
||||||
return filename.substr(start_pos + 1);
|
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
|
Parses a string between "first" and "last" characters
|
||||||
and ignoring ok_chars as whitespaces. The value is
|
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)
|
/// @return extension of filename (ex: filename.txt will return txt)
|
||||||
std::string findExtension(const std::string &filename);
|
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
|
/// returns string between character first and last
|
||||||
int getStringBetween(std::string& out, const char *instr,
|
int getStringBetween(std::string& out, const char *instr,
|
||||||
char first, char last,
|
char first, char last,
|
||||||
|
|
Loading…
Reference in a new issue