3

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.

Community
  • 1
  • 1
Jim Gorski
  • 360
  • 1
  • 5
  • 16
  • If the question asks for C++, why using a Java tag? And this has nothing to do with operator overloading. – Yanick Rochon Oct 26 '12 at 02:39
  • The java part is for a project im working on in Processing where my you can only move if the value in the tile map in the direction you want to move isnt equal to 4,5,6,7,8 and I came across the same problem in my c++ program. As to the operating overloading tag came up because it kept showing up in my google searches when I was searching for this answer. – Jim Gorski Oct 26 '12 at 02:49

6 Answers6

8

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)))
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

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.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Basically i want to set this bool to true if every character in the string is a number 0-9 so should i use '0'? – Jim Gorski Oct 26 '12 at 02:41
  • @JimGorski: It depends on what you mean by "every character in the string is a number". That isn't clear to me even just in English. But just go and figure it out; it's gonna be one of the two! :-) (The good part here is that the C standard requires that any text encoding puts the numerals `'0'`, ..., `'9'` in a contiguous, increasing sequence, so the same inequalities work.) – Kerrek SB Oct 26 '12 at 02:42
  • if the string was "0983" i want it to be true, – Jim Gorski Oct 26 '12 at 02:43
  • but if it was "124Test" i want it to remain false – Jim Gorski Oct 26 '12 at 02:43
  • @JimGorski: Then change it to `'0'` and `'9'`. – Kerrek SB Oct 26 '12 at 02:44
0

You could use collection membership:

!boost::empty(boost::find<boost::return_found_end>("0123456789", string.at(i)))
Mankarse
  • 39,818
  • 11
  • 97
  • 141
0

If the string was "0983" I want it to be true but if it was "124Test" I want it to remain false.

Saying that, an approach would be checking if a character is not a number, then return false, instead of checking every character until the end of the string.

bool b = true;
for(int i = 0; i < string.size(); i++)
{
    if(string.at(i) < '0' || string.at(i) > '9')
    {
        b = false;
        break;
    }
}
Toribio
  • 3,963
  • 3
  • 34
  • 48
0

For the C++ answer, look at this question which has already solved a quite similar problem, that you can very easily adapt to your situation.

As for Java, you can do this :

public boolean isInteger(String s) {
    return s.matches("^[0-9]+$");
}

You can modify the regex to suite your requirements. For example : "^[4-8]+$".

Note: String.matches is not optimal. If you need to perform checks often, use a compiled pattern instead :

static final Pattern DIGITS = Pattern.compile("^[0-9]+$");

public void isInteger(String s) {
    return DIGITS.matcher(s).find();
}
Community
  • 1
  • 1
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
0

It's clumsy enough to use that an ad hoc solution may be easier to use, but the standard library actually supports this directly:

#include <locale>
#include <iostream>
#include <iomanip>

int main() {

    char *inputs[] = { 
        "0983",
        "124test"
    };

    std::locale loc(std::locale::classic());

    std::ctype_base::mask m = std::ctype_base::digit;

    for (int i=0; i<2; i++) {
        char const *b = inputs[i];
        char const *e = b + strlen(b);

        std::cout << "Input: " << std::setw(10) << inputs[i] << ":\t";

        if (std::use_facet<std::ctype<char> >(loc).scan_not(m, b, e) == e)
            std::cout << "All Digits\n";
        else
            std::cout << "Non digit\n";
    }
    return 0;
}

If you're using C++11, std::all_of is almost certainly easier to use:

#include <string>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ctype.h>

int main() {
    std::string inputs[] = {
        "0983",
        "124test"
    };

    std::cout << std::boolalpha;

    for (int i=0; i<2; i++)
        std::cout << std::setw(10) << inputs[i] << "\tAll digits?: "
            << std::all_of(inputs[i].begin(), inputs[i].end(), ::isdigit) << "\n";
    return 0;
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111