c# 检测一串字符串是否为ipv4

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38204686/article/details/82261880
        static bool ifip(string ip)
        {
            int num;
            //分割成四段
            string[] ip_4 = ip.Split('.');
            if (ip_4.Length != 4) return false;
            for (int i = 0; i < 4; i++)
            {
                if (!int.TryParse(ip_4[i], out num)) return false;
                if (num < 0 && num > 255) return false;
            }
            //全部检查完毕 无错误 
            return true;
        }
        static bool ifIp(string a)
        {
            return Regex.IsMatch(a, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

猜你喜欢

转载自blog.csdn.net/qq_38204686/article/details/82261880