Qt - splitting a QString, using several types of whitespace as separators

user129186 picture user129186 · Apr 16, 2016 · Viewed 15k times · Source

I'm looking to split a QString. There are several words in the QString, separated by one or more(!) of the following symbols:

  • whitespace
  • tab
  • CR
  • LF

I'm looking to extract the words only. Basically, I'm trying to replicate the behavior of the Python str.split() function.

I know I can use a regular expression in order to achieve this, but what would it look like? Any other straightforward methods to achieve this are welcome as well.

Answer

Wiktor Stribiżew picture Wiktor Stribiżew · Apr 16, 2016

Note that CR, LF and tab are already whitespace. If you need to match a whitespace you can rely on a shorthand character class \s:

\s Matches a whitespace character (QChar::isSpace()).

So, use then

QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);

If you plan to split a string with specific characters, use a character class.

[...] Sets of characters can be represented in square brackets, similar to full regexps. Within the character class, like outside, backslash has no special meaning.

Then, try

QStringList list = str.split(QRegExp("[\r\n\t ]+"), QString::SkipEmptyParts);

You can enlarge the list later when requirements change.