How to get a first word of from a Qstring

qregexp, qstring, qt

Solution

Use the split function of `QString` in this way:

QString firstWord = string1.split(" ").at(0);

If there is no whitespace in the string, the whole string will be returned.

Problem

I want to get first word of a `Qstring`. For example `String1 = "Read from file1"`. I want to extract `string2 = "Read"`. I want to extract substring based on whitespace. If I encounter a first whitespace in my `string1`, I need that part of `string1` to `string2`.

Original source