本文整理汇总了C++中std::string::replace方法的典型用法代码示例。如果您正苦于以下问题:C++ string::replace方法的具体用法?C++ string::replace怎么用?C++ string::replace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::string
的用法示例。
在下文中一共展示了string::replace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: changenth
bool changenth(std::string &str, int n, const char *with, bool insert = false, bool nonzero = false)
{
std::string::size_type s, e;
if (!findnth(str, n, s, e))
return false;
if (nonzero && str.substr(s, e-s) == "0")
return true; // not an error
if (!insert)
str.replace(s, e-s, with);
else
str.insert(s, with);
return true;
}
示例2: HLE_IPC_BuildFilename
// This is used by several of the FileIO and /dev/fs functions
std::string HLE_IPC_BuildFilename(std::string path_wii)
{
std::string path_full = File::GetUserPath(D_SESSION_WIIROOT_IDX);
// Replaces chars that FAT32 can't support with strings defined in /sys/replace
for (auto& replacement : replacements)
{
for (size_t j = 0; (j = path_wii.find(replacement.first, j)) != path_wii.npos; ++j)
path_wii.replace(j, 1, replacement.second);
}
path_full += path_wii;
return path_full;
}
示例3: replaceString
bool replaceString(std::string& text, const std::string& key, const std::string& value)
{
if(text.find(key) == std::string::npos)
return false;
std::string::size_type start = 0, pos = 0;
while((start = text.find(key, pos)) != std::string::npos)
{
text.replace(start, key.size(), value);
//text = text.substr(0, start) + value + text.substr(start + key.size());
pos = start + value.size();
}
return true;
}
示例4: findandreplace
void findandreplace( std::string& tInput, std::string tFind, std::string tReplace ) {
size_t uPos = 0;
size_t uFindLen = tFind.length();
size_t uReplaceLen = tReplace.length();
if( uFindLen == 0 ){
return;
}
for( ;(uPos = tInput.find( tFind, uPos )) != std::string::npos; ){
tInput.replace( uPos, uFindLen, tReplace );
uPos += uReplaceLen;
}
}
示例5: replaceString
bool replaceString(std::string& text, const std::string key, const std::string value)
{
std::string::size_type start = text.find(key);
if(start == std::string::npos) //skip if there's no key in text
return false;
std::string::size_type pos = 0;
for(; start != std::string::npos; start = text.find(key, pos))
{
text.replace(start, key.size(), value);
pos = start + key.size();
}
return true;
}
示例6: Replace
BOOL URLParser::Replace(std::string& strSource, std::string& strToFind, std::string& ptrToReplace)
{
BOOL bRetVal = FALSE;
while(TRUE)
{
int iPos = strSource.find(strToFind, 0);
if(std::string::npos == iPos)
{
break;
}
strSource.replace(iPos, strToFind.length(), ptrToReplace);
bRetVal = TRUE;
}
return bRetVal;
}
示例7: ReplaceAll
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest)
{
size_t pos = 0;
if (src == dest)
return result;
while ((pos = result.find(src, pos)) != std::string::npos)
{
result.replace(pos, src.size(), dest);
pos += dest.length();
}
return result;
}
示例8: replaceDefines
static void replaceDefines(const std::string& compileTimeDefines, std::string& out)
{
// Replace semicolons with '#define ... \n'
if (compileTimeDefines.size() > 0)
{
size_t pos;
out = compileTimeDefines;
out.insert(0, "#define ");
while ((pos = out.find(';')) != std::string::npos)
{
out.replace(pos, 1, "\n#define ");
}
out += "\n";
}
}
示例9: unescapeId
// static
void CMIRIAMResourceObject::unescapeId(std::string & id)
{
// We have to convert all %[0-9a-fA-F][0-9a-fA-F] character sequences to utf8 characters.
std::string::size_type pos;
for (pos = 0; pos < id.length(); pos++)
if (id[pos] == '%' &&
id.find_first_not_of("0123456789abcdefABCDEF", pos + 1) > pos + 2)
{
char ascii[2];
ascii[0] = (unsigned char) strtol(id.substr(pos + 1 , 2).c_str(), NULL, 16);
ascii[1] = 0x0;
id.replace(pos, 3, CCopasiXMLInterface::utf8(ascii));
}
}
示例10: ReplaceAll
std::size_t StringExtend::ReplaceAll (std::string& source, const std::string& prototype, const std::string& copy)
{
if(prototype.empty()) {
return 0;
}
std::size_t startpos = 0, counter = 0;
while((startpos = source.find(prototype, startpos)) != std::string::npos) {
source.replace(startpos, prototype.length(), copy);
startpos += copy.length();
counter ++;
}
return counter;
}
示例11: FilterBadWords
//actual filtering
void FilterBadWords(std::string& s)
{
//preconstruct our array, we love speed gain by paying startup time
static auto const& Mapping_CP852 = [] {
std::cout << "PRECONSTRUCT: " << __PRETTY_FUNCTION__ << "\n";
static uint8_t mappings[0x100];
for(unsigned i = 0; i < sizeof(mappings); ++i)
mappings[i] = static_cast<uint8_t>(i);
mappings[0x88 /* ł */] = 'l';
mappings[0xa5 /* ą */] = 'a';
mappings[0xa9 /* ę */] = 'e';
mappings[0x86 /* ć */] = 'c';
mappings[0xbe /* ż */] = 'z';
mappings[0xab /* ź */] = 'z';
mappings[0xa2 /* ó */] = 'o';
mappings[0x98 /* ś */] = 's';
mappings[0xe4 /* ń */] = 'n';
mappings[0x9d /* Ł */] = 'L';
mappings[0xa4 /* Ą */] = 'A';
mappings[0xa8 /* Ę */] = 'E';
mappings[0x8f /* Ć */] = 'C';
mappings[0xbd /* Ż */] = 'Z';
mappings[0x8d /* Ź */] = 'Z';
mappings[0xe0 /* Ó */] = 'O';
mappings[0x97 /* Ś */] = 'S';
mappings[0xe3 /* Ń */] = 'N';
return mappings;
}();
std::string sc(s); // bit redundant work here
for (auto& character : sc) character = Mapping_CP852[(uint8_t)character];
for (auto &badword : badwords) {
size_t pos = sc.find(badword);
size_t size = badword.size();
while (pos != std::string::npos)
{
s.replace ( s.begin() + pos, s.begin() + pos + size, "*");
sc.replace(sc.begin() + pos, sc.begin() + pos + size, "*");
pos = sc.find(badword);
}
}
}
示例12: FixPathCase
bool FixPathCase(std::string& basePath, std::string &path, FixPathCaseBehavior behavior)
{
size_t len = path.size();
if (len == 0)
return true;
if (path[len - 1] == '/')
{
len--;
if (len == 0)
return true;
}
std::string fullPath;
fullPath.reserve(basePath.size() + len + 1);
fullPath.append(basePath);
size_t start = 0;
while (start < len)
{
size_t i = path.find('/', start);
if (i == std::string::npos)
i = len;
if (i > start)
{
std::string component = path.substr(start, i - start);
// Fix case and stop on nonexistant path component
if (FixFilenameCase(fullPath, component) == false) {
// Still counts as success if partial matches allowed or if this
// is the last component and only the ones before it are required
return (behavior == FPC_PARTIAL_ALLOWED || (behavior == FPC_PATH_MUST_EXIST && i >= len));
}
path.replace(start, i - start, component);
fullPath.append(component);
fullPath.append(1, '/');
}
start = i + 1;
}
return true;
}
示例13: debParser
std::string AptCacheFile::debParser(std::string descr)
{
// Policy page on package descriptions
// http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Description
unsigned int i;
string::size_type nlpos=0;
nlpos = descr.find('\n');
// delete first line
if (nlpos != string::npos) {
descr.erase(0, nlpos + 2); // del "\n " too
}
// avoid replacing '\n' for a ' ' after a '.\n' is found
bool removedFullStop = false;
while (nlpos < descr.length()) {
// find the new line position
nlpos = descr.find('\n', nlpos);
if (nlpos == string::npos) {
// if it could not find the new line
// get out of the loop
break;
}
i = nlpos;
// erase the char after '\n' which is always " "
descr.erase(++i, 1);
// remove lines likes this: " .", making it a \n
if (descr[i] == '.') {
descr.erase(i, 1);
nlpos = i;
// don't permit the next round to replace a '\n' to a ' '
removedFullStop = true;
continue;
} else if (descr[i] != ' ' && removedFullStop == false) {
// it's not a line to be verbatim displayed
// So it's a paragraph let's replace '\n' with a ' '
// replace new line with " "
descr.replace(nlpos, 1, " ");
}
removedFullStop = false;
nlpos++;
}
return descr;
}
示例14: escapeXMLChars
void escapeXMLChars(std::string &s)
{
std::string bad = "<>&";
const char *replace[] =
{
"<", ">", "&"
};
for (unsigned i = 0; i < s.size(); i++)
{
unsigned n = bad.find(s[i]);
if (n != std::string::npos)
{
s.replace(i, 1, replace[n]);
}
}
}
示例15: replaceAll
static void replaceAll(std::string& str,
const std::string& from,
const std::string& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos)
{
//size_t end_pos = start_pos + from.length();
while((start_pos = str.find(from, start_pos)) != std::string::npos)
{
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
start_pos += to.length();
}
}