6.0.0-rc4Source

QStringTokenizer Class

template <typename Haystack, typename Needle> class QStringTokenizer

The QStringTokenizer class splits strings into tokens along given separators. More…

Header#include <QStringTokenizer>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Since6.0

List of all members, including inherited members

Public Types

typedefvalue_type
typedefsize_type
typedefconst_iterator

Public Functions

QStringTokenizer(typename QtPrivate::Tok::Identity<Haystack>::type haystack, typename QtPrivate::Tok::Identity<Needle>::type needle, Qt::SplitBehavior sb = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringTokenizer(typename QtPrivate::Tok::Identity<Haystack>::type haystack, typename QtPrivate::Tok::Identity<Needle>::type needle, Qt::CaseSensitivity cs, Qt::SplitBehavior sb = Qt::KeepEmptyParts)
iteratorbegin() const
iteratorcbegin() const
sentinelend() const
sentinelcend() const
ContainertoContainer(Container &&c = {}) const

Detailed Description

A QStringTokenizer splits a string lazily: the tokens are QStringView objects into the string, produced one at a time while iterating, so no list is built and nothing is copied.

for (QStringView part : QStringTokenizer{line, u','})
    handle(part.trimmed());

QList<QStringView> parts = qTokenize(line, u';', Qt::SkipEmptyParts).toContainer();

The haystack is viewed when it is an lvalue and moved into the tokenizer when it is a temporary QString, so tokens never dangle while the tokenizer is alive. The needle may be a QChar, a char16_t, a QString, a QStringView or a QLatin1StringView. An empty needle splits between every character, as QString::split() does.

See also QString::split().

Differences from Qt

The tokens are always QStringView objects, also for a QLatin1StringView haystack, which must be converted to a QString first. The iterator is a forward iterator compared against a sentinel returned by end().

Member Type Documentation

typedef QStringTokenizer::const_iterator

Same as iterator.

typedef QStringTokenizer::size_type

Alias for qsizetype.

typedef QStringTokenizer::value_type

The type of a token, QStringView.

Member Function Documentation

QStringTokenizer::QStringTokenizer(typename QtPrivate::Tok::Identity<Haystack>::type haystack, typename QtPrivate::Tok::Identity<Needle>::type needle, Qt::SplitBehavior sb = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive)

Constructs a tokenizer that splits haystack at each occurrence of needle. sb selects whether empty tokens are produced and cs the case sensitivity of the needle.

QStringTokenizer::QStringTokenizer(typename QtPrivate::Tok::Identity<Haystack>::type haystack, typename QtPrivate::Tok::Identity<Needle>::type needle, Qt::CaseSensitivity cs, Qt::SplitBehavior sb = Qt::KeepEmptyParts)

This is an overloaded function.

Constructs a tokenizer for haystack and needle with the case sensitivity cs and the split behavior sb.

iterator QStringTokenizer::begin() const

Returns an iterator to the first token.

iterator QStringTokenizer::cbegin() const

Same as begin().

sentinel QStringTokenizer::cend() const

Same as end().

sentinel QStringTokenizer::end() const

Returns the sentinel that marks the end of the tokens.

template <typename Container = QList<QStringView>>Container QStringTokenizer::toContainer(Container &&c = {}) const

Appends all tokens to the container c and returns it. The default container is a QList<QStringView>; the views refer into the haystack.