C#檢測字符串是否為正整數


方法一(字符串處理):

public static bool IsNum(string str)
            {
bool blResult = true;//默認狀態下是數字
if(str == "")
                    blResult = false;
else
                {
foreach(char Char in str)
                    {
if(!char.IsNumber(Char))
                        {
                            blResult = false;
break;
                        }
                    }
if(blResult)
                    {
if(int.Parse(str) == 0)
                            blResult = false;
                    }
                }
return blResult;
            }

方法二(正則表達式):

static bool IsNumeric(string str)
        {
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]\d*$");
            return reg1.IsMatch(str);
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM