☰
一文带你了解C++中的字符替换方法
2026/10/2 14:22:32 网站建设 项目流程

一、单个字符替换

1.1 std::replace

代码示例:

1

2

3

4

5

6

7

8

#include <algorithm>

// ...

std::string str ="Hello, World!";

std::replace(str.begin(), str.end(),'o','O');

// str 现在为 "HellO, WOrld!"

1.2 使用循环手动替换

1

2

3

4

5

6

7

8

9

std::string str ="Hello, World!";

for(char& c : str) {

if(c =='o') {

c ='O';

}

}

// str 现在为 "HellO, WOrld!"

1.3 使用正则表达式库(例如,std::regex_replace)

1

2

3

4

5

6

7

8

#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)来实现字符串的替换。下面是一个例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <sstream>

#include <string>

std::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 使用字符数组

也可以使用字符数组来实现字符串的替换。下面是一个例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

#include <iostream>

#include <cstring>

char* 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` 算法来实现字符串的替换的例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include <algorithm>

#include <string>

std::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> 来实现字符串的替换的例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <regex>

#include <string>

std::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" 为 "C++"

std::cout << result << std::endl;// 输出:Hello, C++!

result = replace(str,"[Ww]orld","C++");// 替换所有的 "World" 或 "world" 为 "C++"

std::cout << result << std::endl;// 输出:Hello, C++!

result = replace(str,"[a-zA-Z]+","C++");// 替换所有的单词为 "C++"

std::cout << result << std::endl;// 输出:C++, C++!

result = replace(str,"\\b\\w+\\b","C++");// 同上

std::cout << result << std::endl;// 输出:C++, C++!

}

注意:在使用正则表达式时,需要在字符串中的正则表达式前面加上 "\\"

三、总结

在 C++ 中,可以使用字符串流、字符数组、STL 的算法、正则表达式等方法来实现字符串的替换。根据实际需要,可以选择适合的方法来实现字符串的替换。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询