搜索和替换字符数组 c++ 中出现的所有字符串
Searching and replacing all occurrence of a string in a char array c++
>我有一个字符数组,我想找到特定子字符串的所有位置,然后我将用不同的东西替换所有子字符串。
例如:
char [35] = "the boy stole the cup from the table.";
1.我想打印出"the"
所在的每个位置。我找到了一些像find
这样的函数,但它只找到了我想要的第一个位置。我尝试使用循环,但这也没有奏效。
- 我还想用类似
"that"
的东西替换所有出现的"the"
有人可以告诉我如何实现这一目标。我专门使用 char 数组而不是字符串类。
试试这个函数。 我认为它会起作用。
void find_and_replace(string& source, string const& find, string const& replace)
{
for(string::size_type i = 0; (i = source.find(find, i)) != string::npos;)
{
source.replace(i, find.length(), replace);
i += replace.length();
}
}
content = "this is c++ programming";
find_and_replace(content, "c++", "java");
相关文章:
- 如何使用regex_replace将子字符串替换为字符串中的另一个子字符串,同时保持整个文本完整?
- 使用C++将一个字符串替换为另一个字符串
- 简单的字符串替换给出错误,尽管参数正确
- UnicodeString 将一个子字符串替换为另一个子字符串
- C++字符串.替换生成"No matching function for call"错误
- std::字符串替换不保留结尾?
- GCC Bug,使用尖括号通过宏包含标头时"linux"路径中的字符串"<>"替换为 "1"
- 字符串替换问题
- 将带双引号的字符串替换为带双引号的字符串
- 如何在C++中将字符串替换为"Match case"和"Match whole word"
- 字符串替换和奇怪的字符
- C++中的字符串反转不将旧字符串替换为新字符串
- 用c++字符串替换snprintf
- 字符串::替换在有效的迭代器上抛出 std::out_of_range
- 简单的标准::字符串替换在C++
- C++字符串替换函数无限循环
- 分析字符串替换内存泄漏
- C++:-用另一个字符串替换一段字符串
- 提升字符串替换不会用字符串替换换行符
- C++中任意长度字符串的字符串替换