How to check if char* is a number
-
I've got a function that returns a char* type and I need to check if this contains a number or some other type of characters. How could I do this? I've been checking the articles/messages but I couldn't find an example... Thanks!
You may use the strtol[^] function. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You may use the strtol[^] function. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
oops, I removed the message before seeing a reply was given. I found a way anyhow, checking the ascii code
const char \* pcField = sFileNumber.c\_str(); int a = int(\*pcField); if (a >= 48 && a <= 57) //...
bad, bad programming. :^)
Watched code never compiles.
-
oops, I removed the message before seeing a reply was given. I found a way anyhow, checking the ascii code
const char \* pcField = sFileNumber.c\_str(); int a = int(\*pcField); if (a >= 48 && a <= 57) //...
better to use "isdigit" and related similar functions ("isalpha", "isspace", etc...)
-
bad, bad programming. :^)
Watched code never compiles.
-
ok, ok. I'll use isdigit(). I know my solution is not good looking, but, can I ask why is it bad programming?
Maybe not that bad. Ugly, neverthless. Anyway you're just checking the first character of the string. Is this OK (i.e. the string will always contain at most 1 digit)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Maybe not that bad. Ugly, neverthless. Anyway you're just checking the first character of the string. Is this OK (i.e. the string will always contain at most 1 digit)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
err... no, it may contain more than one digit. I see what you mean, it could be a character-string started by a number... Highly improbable in my application, but I should check it anyway, shouldn't I?
Simplest:
bool IsDigits(const std::string& str)
{
return str.empty() ? false : str.find_first_not_of("0123456789") == str.npos;
}More efficient:
bool IsDigits(const std::string& str)
{
return str.empty() ? false : std::find_if_not(str.begin(), str.end(), isdigit) == str.end();
}cheers, AR Edit: changed function name to
IsDigits
and added second version. ReEdit: added empty string handling.When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Thursday, November 4, 2010 1:40 PM