STRING MANIPULATION
-
A down and dirty way is to convert the text to a number in a try-catch block. If an exception is thrown, then .... I'm trapping on the KeyPressed event and checking each key pressed. If it isn't the key I want, I disregard it. If anyone has a better way of doing this, I would like to see it. Larry J. Siddens
-
As the previous reply mentioned, you can do something like this:
public bool IsNumeric(TextBox tb)
{
try
{
Int32.Parse(tb.Parse);
}
catch
{
return false;
}
}If you need to check larger values or decimal values, you could replace
Int32
withInt64
(long
) orDouble
(double
), respectively. If you want to allow other characters like a group separator ("." or ",", depending on the current culture), currenty symbols, etc., use theInt32.Parse(string, NumberStyles)
overload and see the documentation for theNumberStyles
enum for more information. Also, in the future please do not use all caps. Most times, it's considered flaming (yelling at someone) - which most likely you didn't intend - but it still makes it hard to read.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }
-
What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }
Sure, but keep in mind that regular expressions are inherently slower than simple string parsing. If you will be using this regex several times, you should at least compile it. This will boost performance a bit, but it will still be slower than just parsing a string, especially in such an easy case.
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
As the previous reply mentioned, you can do something like this:
public bool IsNumeric(TextBox tb)
{
try
{
Int32.Parse(tb.Parse);
}
catch
{
return false;
}
}If you need to check larger values or decimal values, you could replace
Int32
withInt64
(long
) orDouble
(double
), respectively. If you want to allow other characters like a group separator ("." or ",", depending on the current culture), currenty symbols, etc., use theInt32.Parse(string, NumberStyles)
overload and see the documentation for theNumberStyles
enum for more information. Also, in the future please do not use all caps. Most times, it's considered flaming (yelling at someone) - which most likely you didn't intend - but it still makes it hard to read.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Heath, How you doing? Have you used the method "TryParse"? I see that as a method in the System.Double class. Larry J. Siddens
-
Heath, How you doing? Have you used the method "TryParse"? I see that as a method in the System.Double class. Larry J. Siddens
If you read the documentation, this is just like
Parse
but it try/catches internally and returns a boolean if the value could be successfully parsed. If it could, theout
param contains the value, something similar to this:public static bool TryParse(string s, NumberStyles style,
IFormatProvider provider, out double result)
{
try
{
result = double.Parse(s, style, provider);
}
catch
{
return false;
}
}It just depends on whichever you want to use. If you don't want to handle exceptions yourself, use
Double.TryParse
.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
If you read the documentation, this is just like
Parse
but it try/catches internally and returns a boolean if the value could be successfully parsed. If it could, theout
param contains the value, something similar to this:public static bool TryParse(string s, NumberStyles style,
IFormatProvider provider, out double result)
{
try
{
result = double.Parse(s, style, provider);
}
catch
{
return false;
}
}It just depends on whichever you want to use. If you don't want to handle exceptions yourself, use
Double.TryParse
.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thanks for the reply Heath! Larry J. Siddens
-
What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }
Jose Fco Bonnin wrote: @"0*[1-9][0-9]*" WTF? Firstly, why not use @"\d+" ? And what is the purpose of the "prepended" zero's? :confused: And lastly, why test it again (not correctly even!) ? Very counter-productive... leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it. -
What about using regular expressions? Something like: public bool IsNaturalNumber(string sValue) { return (Regex.IsMatch(sValue, @"0*[1-9][0-9]*") && !Regex.IsMatch(sValue,@"[^0-9]")); }
-
Jose Fco Bonnin wrote: @"0*[1-9][0-9]*" WTF? Firstly, why not use @"\d+" ? And what is the purpose of the "prepended" zero's? :confused: And lastly, why test it again (not correctly even!) ? Very counter-productive... leppie::AllocCPArticle("Zee blog");
Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.if you use @"\d+" and your string is "aaaa1" the result will be true.
-
if you use @"\d+" and your string is "aaaa1" the result will be true.