Get platform specific end of line character in C++/Qt

后端 未结 4 1459
孤城傲影
孤城傲影 2021-01-18 17:27

Is there anything for getting right end-of-line symbol for any platform? I mean, I can use \\n for Windows and Unix if I want to write EOL to file, but there is

4条回答
  •  [愿得一人]
    2021-01-18 17:54

    Try

    QString::split(QRegularExpression{R"-((\r\n?|\n))-"})
    

    This uses a C++11 raw string literal to create a regex that matches all three possibilities:

    • CR only (Mac)
    • CR+LF (Win)
    • LF (Unix)

    If you can not use C++11, you will have to manually escape the regex:

    "(\\r\\n?|\\n)"
    

    That should do the trick.

提交回复
热议问题