如果s
是std::string
,那么是否有如下函数?
s.replace("text to replace", "new text");
答案 0 :(得分:77)
尝试std::string::find
和std::string::replace
的组合。
这得到了这个位置:
std::string s;
std::string toReplace("text to replace");
size_t pos = s.find(toReplace);
这取代了第一个事件:
s.replace(pos, toReplace.length(), "new text");
现在您只需为方便起见创建一个功能:
std::string replaceFirstOccurrence(
std::string& s,
const std::string& toReplace,
const std::string& replaceWith)
{
std::size_t pos = s.find(toReplace);
if (pos == std::string::npos) return s;
return s.replace(pos, toReplace.length(), replaceWith);
}
答案 1 :(得分:24)
我们真的需要一个看似如此简单的任务的Boost库吗?
要替换子串的所有出现,请使用此函数:
std::string ReplaceString(std::string subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
如果你需要性能,这里是一个修改输入字符串的优化函数,它不会创建字符串的副本:
void ReplaceStringInPlace(std::string& subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}
试验:
std::string input = "abc abc def";
std::cout << "Input string: " << input << std::endl;
std::cout << "ReplaceString() return value: "
<< ReplaceString(input, "bc", "!!") << std::endl;
std::cout << "ReplaceString() input string not modified: "
<< input << std::endl;
ReplaceStringInPlace(input, "bc", "??");
std::cout << "ReplaceStringInPlace() input string modified: "
<< input << std::endl;
输出:
Input string: abc abc def
ReplaceString() return value: a!! a!! def
ReplaceString() input string not modified: abc abc def
ReplaceStringInPlace() input string modified: a?? a?? def
答案 2 :(得分:23)
是:replace_all
是提升字符串算法之一:
虽然它不是标准库,但它在标准库上有一些东西:
replace_all
内的trim
)。这对于标准库函数来说更为复杂。答案 3 :(得分:10)
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("one three two four");
string str2("three");
str.replace(str.find(str2),str2.length(),"five");
cout << str << endl;
return 0;
}
one five two four
答案 4 :(得分:7)
就像有人说boost :: replace_all
这是一个虚拟的例子:
#include <boost/algorithm/string/replace.hpp>
std::string path("file.gz");
boost::replace_all(path, ".gz", ".zip");
答案 5 :(得分:2)
不完全是这样,但std::string
有许多replace
重载函数。
浏览this link以查看每种解释,并举例说明如何使用它们。
此外,还有几个版本的string::find
函数(如下所列),您可以将它们与string::replace
结合使用。
另请注意,replace
提供了<algorithm>
个功能的多个版本,您也可以使用它们(而不是string::replace
):
答案 6 :(得分:2)
// replaced text will be in buffer.
void Replace(char* buffer, const char* source, const char* oldStr, const char* newStr)
{
if(buffer==NULL || source == NULL || oldStr == NULL || newStr == NULL) return;
int slen = strlen(source);
int olen = strlen(oldStr);
int nlen = strlen(newStr);
if(olen>slen) return;
int ix=0;
for(int i=0;i<slen;i++)
{
if(oldStr[0] == source[i])
{
bool found = true;
for(int j=1;j<olen;j++)
{
if(source[i+j]!=oldStr[j])
{
found = false;
break;
}
}
if(found)
{
for(int j=0;j<nlen;j++)
buffer[ix++] = newStr[j];
i+=(olen-1);
}
else
{
buffer[ix++] = source[i];
}
}
else
{
buffer[ix++] = source[i];
}
}
}
答案 7 :(得分:0)
这是我最终编写的版本,它替换了给定字符串中目标字符串的所有实例。适用于任何字符串类型。
template <typename T, typename U>
T &replace (
T &str,
const U &from,
const U &to)
{
size_t pos;
size_t offset = 0;
const size_t increment = to.size();
while ((pos = str.find(from, offset)) != T::npos)
{
str.replace(pos, from.size(), to);
offset = pos + increment;
}
return str;
}
示例:
auto foo = "this is a test"s;
replace(foo, "is"s, "wis"s);
cout << foo;
输出:
thwis wis a test
请注意,即使搜索字符串出现在替换字符串中,也可以正常工作。
答案 8 :(得分:0)
void replace(char *str, char *strFnd, char *strRep)
{
for (int i = 0; i < strlen(str); i++)
{
int npos = -1, j, k;
if (str[i] == strFnd[0])
{
for (j = 1, k = i+1; j < strlen(strFnd); j++)
if (str[k++] != strFnd[j])
break;
npos = i;
}
if (npos != -1)
for (j = 0, k = npos; j < strlen(strRep); j++)
str[k++] = strRep[j];
}
}
int main()
{
char pst1[] = "There is a wrong message";
char pfnd[] = "wrong";
char prep[] = "right";
cout << "\nintial:" << pst1;
replace(pst1, pfnd, prep);
cout << "\nfinal : " << pst1;
return 0;
}
答案 9 :(得分:0)
void replaceAll(std::string & data, const std::string &toSearch, const std::string &replaceStr)
{
// Get the first occurrence
size_t pos = data.find(toSearch);
// Repeat till end is reached
while( pos != std::string::npos)
{
// Replace this occurrence of Sub String
data.replace(pos, toSearch.size(), replaceStr);
// Get the next occurrence from the current position
pos =data.find(toSearch, pos + replaceStr.size());
}
}
更多CPP实用程序:https://github.com/Heyshubham/CPP-Utitlities/blob/master/src/MEString.cpp#L60