How to check if my string only numeric?

Bool IsAllDigits(string s) { foreach (char c in s) { if (!Char. IsDigit(c)) return false; } return true; } Or just use LINQ: bool IsAllDigits(string s) { return s. All(Char.

IsDigit); }.

You could use Regex or int.TryParse. See also C# Equivalent of VB's IsNumeric().

1 Although I disagree with the article's recommendation to avoid using library functions offered in the Microsoft. VisualBasic namespace. It's not legacy code or anything like that.

If it does what you want more easily than the other solutions, use it. – Cody Gray Dec 24 '10 at 7:57 1 I wouldn't suggest using int. TryParse or long.

TryParse, since they can overflow. – Jim Mischel Dec 24 '10 at 9:07.

Int.TryParse() method will return false for non numeric strings.

2 It will also throw an overflow exception if the string is too long. – Jim Mischel Dec 24 '10 at 9:07.

Your question is not clear. Is . Allowed in the string?

Is ¼ allowed? String source = GetTheString(); //only 0-9 allowed in the string, which almost equals to int. TryParse bool allDigits = source.

All(char. IsDigit); bool alternative = int. TryParse(source,out result); //allow other "numbers" like ¼ bool allNumbers = source.

All(char. IsNumber).

If you want to use Regex you would have to use something like this: string regExPattern = @"^0-9+$"; System.Text. RegularExpressions. Regex pattern = new System.Text.

RegularExpressions. Regex(regExPattern); return pattern. IsMatch(yourString).

You cod do like that: public bool IsNumeric(string val) { if(int. TryParse(val)) return true; else if(double. TryParse(val)) return true; else if(float.

TryParse(val)) return true; else return false; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions