在 c++ 中将一个 ascii 字符的所有实例替换为另一个实例
Replacing all instances of one ascii character with another in c++?
可能的重复项:
如何替换字符串中出现的所有字符?
例如,我有一个字符串"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
}
相关文章:
- 从C++实例化QML
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 为什么 Serial.println(<char[]>);返回随机字符?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 我的字符计数代码计算错误.为什么
- 如何创建具有不同字符数组的多个类实例?
- 如何在C++中动态分配全局字符数组中的类实例?
- 从字符串中删除字符的第一个和最后一个实例
- C++/CX - 我需要从常量字符*实例化一个平台::字符串
- 从字符* 高效实例化向量<char>
- 避免针对不同的字符数组大小进行模板实例化
- 在不同实例中,十六进制值通过字符指针使用不同的填充进行打印
- 为什么要使用静态字符缓冲区来实例化从DLL加载的C++类
- 在 c++ 中将一个 ascii 字符的所有实例替换为另一个实例
- 为什么字符大小作为实例变量增加
- c++删除(0,1);在字符串中,删除文件中字符的所有实例