2

Here I have a string like this WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB I want to delete all wub word form this and get this result WE ARE THE CHAMPIONS MY FRIEND.
Is there any particular function in c++ to do that I found string::erase but that relay doesn't help me!!
I can do that with for loop and find this word form string then delete it but I'm looking for better way. Is there any function to do this???

2
  • 2
    You could use std::regex_replace ? Commented Sep 24, 2014 at 14:42
  • Or boost::algorithm::replace_all Commented Sep 24, 2014 at 14:43

3 Answers 3

7

You could use std::regex_replace:

std::string s1 = "WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB";
std::regex target("(WUB)+");
std::string replacement = " ";
std::string s2 = std::regex_replace(s1, target, replacement);

LIVE DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

Given the OP's desired output, I think the replacement string should contain a space.
Ah, and the updated regex replaces consecutive matches with a single space. Good work.
@Paul R Thanks for your answer it worked prefectly but there should be space between each word in output
@Daniel.V: I've updated the answer in the last minute or so to fix this - you might need to refresh the page in your browser?
4

Use boost::algorithm::replace_all

#include <iostream>
#include <string>
#include <boost/algorithm/string/replace.hpp>

int main()
{
    std::string input="WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB";
    boost::algorithm::replace_all(input, "WUB", " ");
    std::cout << input << std::endl;
    return 0;
}

5 Comments

Could you expand your answer to give an example of how this would be applied ?
Thanks - that's helpful. I guess this wouldn't cope with the OP's example exactly though, i.e. WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB -> WE ARE THE CHAMPIONS MY FRIEND ?
@PaulR we have to initialize input with "WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB" and replacement with "WUB" in place of CD.
Sure, but the "WUBWUB" between "ARE" and "THE" will give two spaces, not one ?
@PaulR Yes. I got the point now. regex_replace is the better option.
1

Erase all occurences:

#include <iostream>

std::string removeAll( std::string str, const std::string& from) {
    size_t start_pos = 0;
    while( ( start_pos = str.find( from)) != std::string::npos) {
        str.erase( start_pos, from.length());
    }
    return str;
}

int main() {

    std::string s = "WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB";
    s = removeAll( s, "WUB");

    return 0;
}

http://ideone.com/Hg7Kwa


Replace all occurences:

std::string replaceAll(std::string str, const std::string& from, const std::string& to) {
    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();
    }
    return str;
}

int main() {
    std::string s = "WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB";
    s = replaceAll( s, "WUB", " ");
    /* replace spaces */
    s = replaceAll( s, "  ", " ");

    return 0;
}

http://ideone.com/Yc8rGv

3 Comments

From the example in the question it looks like the OP wants to replace one ore more consecutive occurrences of "WUB" with a single space.
Looks good, except "WUBWUB" should only give one space, I think, not two ?
Cool: +1 for persistence. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.