用 std::string
中的另一个字符替换所有出现的字符的有效方法是什么?
原文由 big-z 发布,翻译遵循 CC BY-SA 4.0 许可协议
用 std::string
中的另一个字符替换所有出现的字符的有效方法是什么?
原文由 big-z 发布,翻译遵循 CC BY-SA 4.0 许可协议
如何仅使用旧的 C 字符串函数将任何字符串替换为任何字符串?
char original[256]="First Line\nNext Line\n", dest[256]="";
char* replace_this = "\n"; // this is now a single character but could be any string
char* with_this = "\r\n"; // this is 2 characters but could be of any length
/* get the first token */
char* token = strtok(original, replace_this);
/* walk through other tokens */
while (token != NULL) {
strcat(dest, token);
strcat(dest, with_this);
token = strtok(NULL, replace_this);
}
dest
现在应该有我们正在寻找的东西。
原文由 chara 发布,翻译遵循 CC BY-SA 4.0 许可协议
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
3 回答3.4k 阅读
1 回答1.6k 阅读✓ 已解决
std::string
不包含此类功能,但您可以使用独立的replace
功能来自algorithm
标头。