26

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?

AAEM
  • 1,837
  • 2
  • 18
  • 26
Wes
  • 4,781
  • 7
  • 44
  • 53
  • What is your definition of "more elegant"? You surely have to look at every character in one or another way, no matter if hidden behind some function or in a custom loop. – PlasmaHH Jan 09 '12 at 16:25
  • 1
    @Wes. If I can give you an advice, it will be to take 1 or 2 solid days to learn regular expressions. Their syntax is pretty much the same in all languages. I noticed that some of your questions are similar to this one. When you will possess the power of regexp, you will be able to do some powerful stuff you didn't think about first. Just saying. – UmNyobe Jan 09 '12 at 17:40
  • Yes, I think this would be a good idea too. I just came across them recently. I think I need practice. Good idea UmNyobe. Thanks! – Wes Jan 09 '12 at 18:08
  • @UmNyobe I agree with you, i just spent them – AAEM May 03 '18 at 00:07

3 Answers3

34

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";
feedc0de
  • 3,646
  • 8
  • 30
  • 55
sth
  • 222,467
  • 53
  • 283
  • 367
  • 1
    Thanks for the reply. Question though: Why is it "\\d*" and not just "\d*" ? – Wes Jan 09 '12 at 17:04
  • 5
    Because when you write a string in C, \ has special meaning. For example `"\n"` is a newline, not a backslash character followed by the letter "n". If you really want a literal backslash character, you have to write \\. – sth Jan 09 '12 at 17:08
  • 4
    `^` and `$` not needed because of exactMatch and watch out for signs! Then: `[-+]?[0-9]*` – AdUki Apr 29 '15 at 09:05
  • @AdUki A dash is not a numeric character, so that renders the test incorrect. – Johannes Schaub - litb Jul 22 '17 at 19:22
  • @JohannesSchaub-litb Incorrect. The square brackets signify a character class, the dash signifies 'in the range'. So: a character in the range of 0 to 9. – JBES Jan 26 '19 at 15:12
  • @JBE don't understand. What range does "-+" signify? – Johannes Schaub - litb Jan 26 '19 at 15:55
  • @JohannesSchaub-litb `[-+]` matches '+' or '-' literally. The '-' is not placed within any characters. Try for yourself at [Regex101.com](https://regex101.com/) and learn more at [Perl.org](https://perldoc.perl.org/perlre.html). See _The Basics_ (QRegExp is based on Perlre). – JBES Jan 26 '19 at 16:43
  • @JBED isn't that what I said? How can a numeric character consist of two characters? "-1" is not a character. PS I would recommend QReguparExpression instead. The other one is deprecated, I think – Johannes Schaub - litb Jan 27 '19 at 08:10
  • I'm not really following you. See my comment at 15:12. There is no other way of explaining it. If you really don't understand I recommend reading the linked Perl document above. – JBES Mar 03 '19 at 00:01
28

QString::​toInt Is what you looking for .

int QString::​toInt(bool * ok = 0, int base = 10) const

Returns the string converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails. If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.

Example :

QString str = "FF";
bool ok;
int hex = str.toInt(&ok, 16);       // hex == 255, ok == true
int dec = str.toInt(&ok, 10);       // dec == 0, ok == false
Alexander
  • 12,424
  • 5
  • 59
  • 76
  • 3
    It works only for numbers less than maximum integer value 2,147,483,647 – Libor B. Jun 07 '18 at 09:15
  • what if the number is 0 ? it will return 0 and you will think that the conversion failed while it is not true. – Nulik Jan 12 '19 at 23:09
  • 1
    @Nulik in this case you should assert against `ok`, which will be set to `true`. – Alexander Jan 13 '19 at 10:36
  • Regarding the `toInt` magnitude limit - note that `QByteArray` has `toULongLong( &ok )`, and other functions like this. – BuvinJ May 20 '20 at 14:29
5

we can iterate over every character like this code:

QString example = "12345abcd";
for (int i =0;i<example.size();i++)
{
    if (example[i].isDigit()) // to check if it is number!! 
        // do something
    else if (example[i].isLetter()) // to check if it is alphabet !!
        // do something
}