Search code examples
c++stringc++11whitespacestring-parsing

Remove whitespace from string excluding parts between pairs of " and ' C++


So essentially what I want to do is erase all the whitespace from an std::string object, however excluding parts within speech marks and quote marks (so basically strings), eg:

Hello, World! I am a string

Would result in:

Hello,World!Iamastring

However things within speech marks/quote marks would be ignored:

"Hello, World!" I am a string

Would result in:

"Hello, World!"Iamastring

Or:

Hello,' World! I' am a string

Would be:

Hello,' World! I'amastring

Is there a simple routine to perform this to a string, either one build into the standard library or an example of how to write my own? It doesn't have to be the most efficient one possible, as it will only be run once or twice every time the program runs.


Solution

  • No, there is not such a routine ready.

    You may build your own though.

    You have to loop over the string and you want to use a flag. If the flag is true, then you delete the spaces, if it is false, you ignore them. The flag is true when you are not in a part of quotes, else it's false.

    Here is a naive, not widely tested example:

    #include <string>
    #include <iostream>
    using namespace std;
    
    int main() {
        // we will copy the result in new string for simplicity
        // of course you can do it inplace. This takes into account only
        // double quotes. Easy to extent do single ones though!
        string str("\"Hello, World!\" I am a string");
        string new_str = "";
        // flags for when to delete spaces or not
        // 'start' helps you find if you are in an area of double quotes
        // If you are, then don't delete the spaces, otherwise, do delete
        bool delete_spaces = true, start = false;
        for(unsigned int i = 0; i < str.size(); ++i) {
            if(str[i] == '\"') {
                start ? start = false : start = true;
                if(start) {
                    delete_spaces = false;
                }
            }
            if(!start) {
                delete_spaces = true;
            }
            if(delete_spaces) {
                if(str[i] != ' ') {
                    new_str += str[i];
                }
            } else {
                new_str += str[i];
            }
    
        }
        cout << "new_str=|" << new_str << "|\n";
        return 0;
    }
    

    Output:

    new_str=|"Hello, World!"Iamastring|