0

I'm trying to determine if a character falls within a given range. I've searched, but can't find a way to do this.

if(data[0] >= character) {
    // do this
}

The above is just a simplified example of what I am trying to achieve, where data is a string, and character is a char. I will also check to see whether data[0] is less than another character, but have omitted that for this example.

Can somebody guide me on what function to use?

4

1 回答 1

2

What you are doing is fine. A character is an integral type so doing comparisons the way you are is okay.

You could write your own function to do this if you want, but no function exists as far as I know to do this for you.

Something like...

bool charInRange( char toCheck, char min, char max )
{
  return ( toCheck >= min && toCheck <= max );
}

might work for you. You could also remove the equal signs if you want.

Then use it like:

if( charInRange( data[0], 'b', 'h' ) )
{
  //dostuff
}
于 2012-10-07T00:12:16.077 回答