How to get a first word of from a Qstring

mkreddy picture mkreddy · Feb 6, 2014 · Viewed 10.2k times · Source

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.

Answer

Rud Limaverde picture Rud Limaverde · Feb 6, 2014

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.