在c++中,可以使用std::string类来表示字符串,可以使用std::replace函数来替换字符串中的指定字符。下面是一个示
在c++中,可以使用std::string
类来表示字符串,可以使用std::replace
函数来替换字符串中的指定字符。
下面是一个示例代码:cpp
#include
#include
#include
int main() {
std::string str = "Hello, World!";
char oldChar = 'o';
char newChar = '*';
std::replace(str.begin(), str.end(), oldChar, newChar);
std::cout << str << std::endl;
return 0;
}
在上述代码中,我们定义了一个字符串str
,并指定了要替换的字符oldChar
和替换后的字符newChar
。然后,我们使用std::replace
函数来替换字符串中的所有oldChar
字符为newChar
字符。最后,我们输出替换后的字符串。
运行以上代码会输出Hell*, W*rld!
,可以看到字符串中的所有o
字符都被替换为*
字符。
希望对你有所帮助!
--结束END--
本文标题: c++字符串替换指定字符怎么实现
本文链接: https://www.lsjlt.com/news/431863.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0