Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to say != 0-9 in c ++ [duplicate]

Tags:

java

c++

syntax

Possible Duplicate:
how to check if given c++ string or char* contains only digits?

Im trying to say that

   if(string.at(i) != 0-9){
    b= true;
} 

Is there a way to say this without typing value != 0 && value != 1 ...etc? Also if this exists and is possible, and its different in java I also would find that helpful.

Thanks you guys are always helpful.

like image 714
Jim Gorski Avatar asked Sep 14 '25 20:09

Jim Gorski


2 Answers

C++:

#include <cctype>
using namespace std;
...
if (!isdigit(str[i]))

// or

if (str[i] < '0' || str[i] > '9')

Java:

if (!Character.isDigit(str.charAt(i)))
like image 114
John Kugelman Avatar answered Sep 16 '25 11:09

John Kugelman


Say string[i] < 0 || string[i] > 9.

Make sure you actually mean 0 (the value), and not '0' (the character for the numeral for zero). In the latter case (as you suggest in the comment), you want string[i] < '0' || string[i] > '9'. (The numerals are guaranteed to be contiguous and ordered in any text encoding, so this works on any platform.)

like image 29
Kerrek SB Avatar answered Sep 16 '25 09:09

Kerrek SB