Darwinbots Forum

Code center => Darwinbots Program Source Code => Topic started by: Zelos on November 10, 2005, 01:50:26 PM

Title: String Integer Check
Post by: Zelos on November 10, 2005, 01:50:26 PM
Is there a way to check if a string is a integer like if I have a string called stringa and then if it has the value "hi" or "<%" its gives false or something and if it has "5" it returns true?
Title: String Integer Check
Post by: PurpleYouko on November 10, 2005, 03:01:23 PM
no easy way that I know of.

You would have to create a function that seperates all the numbers from the other characters, then if the number of "other" characters is greater than 0 then return false.
Title: String Integer Check
Post by: Numsgil on November 10, 2005, 04:10:44 PM
I think val(string) would return 0 if the string isn't a number, if that's helpful...
Title: String Integer Check
Post by: Ulciscor on November 10, 2005, 09:29:30 PM
"IsNumeric" will return true if the argument can be expressed as an integer and false if it can't...
... but it allows commas and periods in the argument.

Code: [Select]
IsNumeric("hello") = False
IsNumeric("77") = True
IsNumeric("hello77") = False
IsNumeric("77.55") = True
IsNumeric("1,099") = True
Title: String Integer Check
Post by: PurpleYouko on November 11, 2005, 09:42:32 AM
Quote
I think val(string) would return 0 if the string isn't a number, if that's helpful...
In my experience, val(string) crashes if the string isn't a number.  :(
Title: String Integer Check
Post by: PurpleYouko on November 11, 2005, 09:43:18 AM
Quote
"IsNumeric" will return true if the argument can be expressed as an integer and false if it can't...
Didn't know there was such a function. Thanks for the info Ulc.
Title: String Integer Check
Post by: Ulciscor on November 11, 2005, 12:09:36 PM
"val(string)" will return 0 if the argument isn't a number.
Title: String Integer Check
Post by: PurpleYouko on November 11, 2005, 12:20:59 PM
Quote
"val(string)" will return 0 if the argument isnt a number.

Just tested it and you are right. It does return 0.

Must have been thinking about VB for Applications.

No apparently not. Just tested that too and it returns 0

Could have sworn I had seen crashes in this kind of code.  :blink:

Maybe I am just going senile.  :wacko:
Title: String Integer Check
Post by: Ulciscor on November 11, 2005, 12:22:15 PM
Lol :D

I had a go at using "CInt(string)" on non-numerical strings and that indeed crashes as you might expect... was this what you were thinking of?
Title: String Integer Check
Post by: PurpleYouko on November 11, 2005, 12:55:56 PM
quite possibly. I don't tend to use "val" very much as the output format is limited.