在 c++ 中将一个 ascii 字符的所有实例替换为另一个实例

Replacing all instances of one ascii character with another in c++?

本文关键字:实例 字符 ascii 替换 另一个 一个 c++      更新时间:2023-10-16

可能的重复项:
如何替换字符串中出现的所有字符?

例如,我有一个字符串"Hello World",我想将所有"l"替换为"1"。我该怎么做?我是 c++ 的新手。我的大部分背景都是Python,你可以在其中使用.replace 方法。

使用 std::replace

#include <string>
#include <algorithm>
#include <iostream>
int main() {
    std::string str = "Hello World";
    std::replace(str.begin(),str.end(),'l','1');
    std::cout << str; //He11o Wor1d
}