0

Possible Duplicate:
How to replace all occurrences of a character in string?

E.g, I have a string, "Hello World" and I want to replace all the "l"s with "1"s. How would I do this? I am new to c++. Most of my background is in Python in which you could just use the .replace method.

Community
  • 1
  • 1
Logan Shire
  • 5,013
  • 4
  • 29
  • 37

1 Answers1

5

Use 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
}
Rapptz
  • 20,807
  • 5
  • 72
  • 86