简体   繁体   English

如何从 C++ 中的字符串中删除换行符?

[英]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.查看 ASCII 值显示了原因,我的努力仅删除了新行,而不是回车。 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这是我输入 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.如您所见,这些值的末尾是 ASCII 10 和 13,用于创建换行符。 13 is carriage return and 10 is newline. 13 是回车,10 是换行。 The last one has its 10 remove earlier in the code so the code doesn't do an extra loop on an empty substring.最后一个在代码中较早地删除了 10 个,因此代码不会对空子字符串执行额外的循环。 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:我删除换行符的努力,包括注释掉的擦除功能,要么只删除 13,要么删除 10 和 13,但会破坏以后的数据,如下所示:

损坏的控制台

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.此外,您可以看到使用 cout 同时打印外观和单词1会导致外观由于某种原因不存在。 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 :要从字符串中删除所有控制字符,您可以使用std::remove_ifstd::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.删除所有控制字符后,您就可以处理字符串而无需检查它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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