
一、单个字符替换1.1 std::replace代码示例12345678#include algorithm// ...std::string str Hello, World!;std::replace(str.begin(), str.end(),o,O);// str 现在为 HellO, WOrld!1.2 使用循环手动替换123456789std::string str Hello, World!;for(char c : str) {if(c o) {c O;}}// str 现在为 HellO, WOrld!1.3 使用正则表达式库例如std::regex_replace12345678#include regex// ...std::string str Hello, World!;std::string result std::regex_replace(str, std::regex(o),O);// result 现在为 HellO, WOrld!二、字符串替换2.1 实用字符串流可以使用 C 的字符串流stringstream来实现字符串的替换。下面是一个例子1234567891011121314151617181920212223#include sstream#include stringstd::string replace(std::string str,conststd::string from,conststd::string to) {std::stringstream ss;size_tstart_pos 0;while((start_pos str.find(from, start_pos)) ! std::string::npos) {ss str.substr(0, start_pos) to;start_pos from.length();str str.substr(start_pos);}ss str;returnss.str();}intmain() {std::string str Hello, World!;std::string from World;std::string to C;std::string result replace(str, from, to);std::cout result std::endl;// 输出Hello, C!return0;}2.2 使用字符数组也可以使用字符数组来实现字符串的替换。下面是一个例子12345678910111213141516171819202122232425262728293031323334#include iostream#include cstringchar* replace(char* str,constchar* from,constchar* to) {size_tstr_len strlen(str);size_tfrom_len strlen(from);size_tto_len strlen(to);size_tnew_len str_len to_len - from_len;char* new_str newchar[new_len 1];char* p1 str;char* p2 new_str;while(*p1) {if(strncmp(p1, from, from_len) 0) {strncpy(p2, to, to_len);p2 to_len;p1 from_len;}else{*p2 *p1;}}*p2 \0;delete[] str;returnnew_str;}intmain() {charstr[] Hello, World!;constchar* from World;constchar* to C;char* result replace(str, from, to);std::cout result std::endl;// 输出Hello, C!delete[] result;return0;}2.3 使用 STL 的算法std::replace下面是使用 STL 的 replace 算法来实现字符串的替换的例子1234567891011121314151617181920#include algorithm#include stringstd::string replace(std::string str,conststd::string from,conststd::string to) {size_tstart_pos 0;while((start_pos str.find(from, start_pos)) ! std::string::npos) {str.replace(start_pos, from.length(), to);start_pos to.length();}returnstr;}intmain() {std::string str Hello, World!;std::string from World;std::string to C;std::string result replace(str, from, to);std::cout result std::endl;// 输出Hello, C!return0;}2.4 使用正则表达式如果要替换字符串中的多个子串或者要进行更复杂的字符串替换操作可以使用正则表达式。下面是使用 C 的正则表达式库 regex 来实现字符串的替换的例子1234567891011121314151617181920212223#include regex#include stringstd::string replace(std::string str,conststd::string pattern,conststd::string to) {std::regex r(pattern);returnstd::regex_replace(str, r, to);}intmain() {std::string str Hello, World!;std::string str Hello, World!;std::string result replace(str,World,C);// 替换所有的 World 为 Cstd::cout result std::endl;// 输出Hello, C!result replace(str,[Ww]orld,C);// 替换所有的 World 或 world 为 Cstd::cout result std::endl;// 输出Hello, C!result replace(str,[a-zA-Z],C);// 替换所有的单词为 Cstd::cout result std::endl;// 输出C, C!result replace(str,\\b\\w\\b,C);// 同上std::cout result std::endl;// 输出C, C!}注意在使用正则表达式时需要在字符串中的正则表达式前面加上 \\三、总结在 C 中可以使用字符串流、字符数组、STL 的算法、正则表达式等方法来实现字符串的替换。根据实际需要可以选择适合的方法来实现字符串的替换。