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

17,074

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.

Share:
17,074
user129186
Author by

user129186

Updated on June 15, 2022

Comments

  • user129186
    user129186 almost 2 years

    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.