简体   繁体   中英

How can I remove a newline from inside a string in C++?

I am trying to take text input from the user and compare it to a list of values in a text file. The values are this:

价值观

That line at the end is the cursor, not a straight line, but it doesn't matter. Anyway, I sort by word and produce the values, then check the values. Semicolon is a separator between words. All the data is basic to get the code working first. The important thing is that all the pieces of data have newlines after them. No matter what I try, I can't get rid of the newlines completely. Looking at the ASCII values shows why, My efforts remove only the new line, but not the carriage return. This is fine most of the time, but when comparing values they won't be the same because the one with the carriage return is treated as longer. Here is the important parts of the code:

    int pos = 0;
    while (pos != std::string::npos)
    {
        std::string look = lookContents.substr(pos+1, lookContents.find("\n", pos + 1) - pos);
        //look.erase(std::remove(look.begin(), look.end(), '\n'), look.end());
        
        //##
        for (int i = 0; i < look.length(); i++)
        {
            std::cout << (int)(look[i]) << " ";
        }
        std::cout << std::endl;
        std::cout << look << ", " << words[1] << std::endl;
        std::cout << look.compare(0,3,words[1]) << std::endl;
        std::cout << pos << std::endl;
        //##

        //std::cout << look << std::endl;
        if (look == words[1])
        {
            std::cout << pos << std::endl;
            break;
        }
        pos = lookContents.find("\n", pos + 1);
    }

Everything between the //## are just error checking things. Heres what is outputs when I type look b:2

安慰

As you can see, the values have the ASCII 10 and 13 at the end, which is what is used to create newlines. 13 is carriage return and 10 is newline. The last one has its 10 remove earlier in the code so the code doesn't do an extra loop on an empty substring. My efforts to remove the newline, including the commented out erase function, either only remove the 13, or remove both the 10 and 13 but corrupt later data like this:

损坏的控制台

Also, you can see that using cout to print look and words 1 at the same time causes look to just not exist for some reason. Printing it by itself works fine though. I realise I could fix this by just using that compare function in the code to check all but the last characters, but this feels like a temporary fix. Any solutions?

My efforts remove only the new line, but not the carriage return

The newline and carriage control are considered control characters.

To remove all the control characters from the string, you can use std::remove_if along with std::iscntrl :

#include <cctype>
#include <algorithm>
//...
lookContents.erase(std::remove_if(lookContents.begin(), lookContents.end(), 
                  [&](char ch) 
                  { return std::iscntrl(static_cast<unsigned char>(ch));}), 
                  lookContents.end());

Once you have all the control characters removed, then you can process the string without having to check for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM