ymoreau
ymoreau

Reputation: 3976

How to remove empty and null strings from QStringList?

I have a QStringList and I need to clean some meaningless elements : the empty and the null strings.

I could not find a QStringList shortcut function for that.
What is the simplest way to clean the empty/null strings ?

Upvotes: 4

Views: 8922

Answers (2)

The default-constructed QString compares equal to empty and null strings, thus:

inline void removeEmptyAndNull(QStringList &l) {
  l.removeAll({});
}

Upvotes: 7

ymoreau
ymoreau

Reputation: 3976

myQStringList.removeAll(QString("")); // Returns the number of entries removed

Empty string QString("") and null string QString() both return true when compared to an empty string.
Then the test of QList::removeAll(const T &value) will remove both empty and null strings from the list.

Upvotes: 7

Related Questions