QString::split() and "\r", "\n" and "\r\n" convention

c++, qt

Solution

If it's acceptable to remove blank lines, you can try:

QString.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);

This splits the string whenever any of the newline character (either line feed or carriage return) is found. Any consecutive line breaks (e.g. `\r\n\r\n` or `\n\n`) will be considered multiple delimiters with empty parts between them, which will be skipped.

Problem

I understand that `QString::split` should be used to get a `QStringList` from a multiline `QString`. But if I have a file and I don't know if it comes from Mac, Windows or Unix, I'm not sure if `QString.split("\n")` would work well in all the cases. What is the best way to handle this situation?

Original source