How to detect if a QString is made up of all numeric characters?

Wes picture Wes · Jan 9, 2012 · Viewed 34.2k times · Source

What is the best way to tell if a QString is made up of just numbers?

There doesn't appear to be a convenience function in the QString library.

Do I have to iterate over every character, one at a time, or is there a more elegant way that I haven't thought of?

Answer

sth picture sth · Jan 9, 2012

You could use a regular expression, like this:

QRegExp re("\\d*");  // a digit (\d), zero or more times (*)
if (re.exactMatch(somestr))
   qDebug() << "all digits";