0

The replace function allow to change a character from a string, with another. using: replace(s.begin(), s.end(), 'X', 'Y'); But it is possible to change 2 or more different characters with one? for example: In string "Tell us more about your question", I want to change all "o, i, u" with "x". So at output it will be: Tell xs abxxt yxxr qxuestixn

lidya_q
  • 1
  • 1
  • Not a dublicate, this is another question with another sense, before asking it, I spend lot of time to search on stackoverflow my answer – lidya_q Oct 02 '19 at 05:11
  • Dear lidya I think your question is the same as https://stackoverflow.com/questions/37952240/replace-multiple-pair-of-characters-in-string – Nastaran Hakimi Oct 02 '19 at 05:23
  • @NastaranHakimi I will try to use your answer. thx – lidya_q Oct 02 '19 at 14:19

1 Answers1

0

From another answer this should work:

void replaceAll(std::string& str, const std::string& from, const std::string& to) {
    if(from.empty())
        return;
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
    }
}

Credit: Michael Mrozek on SO

BTables
  • 4,413
  • 2
  • 11
  • 30
  • I know that answer, and I don't like it, that's why I'm asking, I don't need to replace a particular range. for example there is an answer, to change "bc" with "!!", my question is, if there it's another function like replace, to take many deiferent characters from a string and replace them with one character – lidya_q Oct 02 '19 at 05:05
  • @lidya_q In that case, the answer is no, there is no built in function to do what you're asking. – BTables Oct 02 '19 at 05:14