How to delete all occurrences of a specific string a string?

时间:2018-06-04 17:38:33

标签: c++ string

How to delete all occurrences of FOO in a string s = FOOTHEFOOFOOBEST for example so the output would be THE BEST without any extra spaces unlike my code does.
Here is an sample of my try:

#include <iostream>
#include <string>
int main() {

    std::string s = "FOOTHEFOOFOOBEST";
        for (int i = 0; i < s.size(); i++){
            if (s[i] == 'F'&&s[i + 1] == 'O' && s[i + 2] == 'O'){
                std::cout << ' ';
                i += 2;
            }
            else
                std::cout << s[i];
        }

        return 0;
}

The output of my code is THE BEST there's an extra space however I expected THE BEST, Is there a simpler way to do that?

1 个答案:

答案 0 :(得分:0)

我想我会这样做:

THE BEST

预期产出:

DECLARE             @Numbers AS INT
SET                 @Numbers = 301
WHILE  @Numbers <= 2300
BEGIN
    INSERT INTO dbo.Numbers VALUES(@Numbers)
    SET @Numbers=@Numbers+1
END