zoukankan      html  css  js  c++  java
  • C#如何检测一个字符串是不是合法的URL



    C#如何检测一个字符串是不是合法的URL

    using System.Text.RegularExpressions;

       /// <summary>
            /// 检测串值是否为合法的网址格式
            /// </summary>
            /// <param name="strValue">要检测的String值</param>
            /// <returns>成功返回true 失败返回false</returns>
            public static bool CheckIsUrlFormat(string strValue)
            {
                return CheckIsFormat(@"(http://)?([w-]+.)+[w-]+(/[w- ./?%&=]*)?"strValue);
            }

            /// <summary>
            /// 检测串值是否为合法的格式
            /// </summary>
            /// <param name="strRegex">正则表达式</param>
            /// <param name="strValue">要检测的String值</param>
            /// <returns>成功返回true 失败返回false</returns>
            public static bool CheckIsFormat(string strRegexstring strValue)
            {
                if (strValue != null && strValue.Trim() != "")
                {
                    Regex re = new Regex(strRegex);
                    if (re.IsMatch(strValue))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                return false;
            }

            private void button1_Click(object senderEventArgs e)
            {
                if (CheckIsUrlFormat(textBox1.Text))
                    MessageBox.Show("YES");
                else
                    MessageBox.Show("No");
            }







    附件列表

    • 相关阅读:
      php面向对象方法
      php数组中元素的操作
      PHP数组的排序
      [设计模式]抽象工厂模式
      [设计模式]建造者模式
      [设计模式]工厂方法模式
      [设计模式]简单工厂模式
      [设计模式]单例模式
      设计模式系列
      JAVA 设计模式 状态模式
    • 原文地址:https://www.cnblogs.com/xe2011/p/3761969.html
    Copyright © 2011-2022 走看看