且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查找和替换字符串中的所有字符与特定符号C ++

更新时间:2023-02-19 12:36:28

标准库已经

#include <algorithm> // for std::replace

std::replace(_deciphered.begin(), _deciphered.end(), 'A', '*');

如果您需要手动进行,请记住, std :: string 看起来像是 char 的容器,所以你可以遍历它的内容,检查每个元素是否 'A',如果是,请将其设置为'*'

If you need to do this by hand, then bear in mind that an std::string looks like a container of char, so you can iterate over its contents, check if each element is 'A', and if so, set it to '*'.

工作示例:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
  std::string s = "FooBarro";
  std::cout << s << std::endl;
  std::replace(s.begin(), s.end(), 'o', '*');
  std::cout << s << std::endl;
}

输出:

FooBarro

F ** Barr *

F**Barr*