Loading

通用类整理

1.返回指定日期格式,有助于统一整个项目的日期格式

返回指定日期格式
        /// <summary>
        
/// 返回指定日期格式
        
/// </summary>
        public static string DatetimeToString(string str)
        {
            return Convert.ToDateTime(str).ToShortDateString();
        }

 

2.获取当前页面的路径

获取页面当前路径
        /// <summary>
        
/// 获取页面当前路径 
         
/// </summary>
        
/// <returns></returns>
        public static string GetRootURI()
        {
            string AppPath = "";
            HttpContext HttpCurrent = HttpContext.Current;
            HttpRequest Req;
            if (HttpCurrent != null)
            {
                Req = HttpCurrent.Request;

                string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
                if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                    //直接安装在   Web   站点 
                    AppPath = UrlAuthority;
                else
                    //安装在虚拟子目录下 
                    AppPath = UrlAuthority + Req.ApplicationPath;
            }
            string str = HttpContext.Current.Server.MapPath("/");
            return AppPath;
        }

3.截取字符

把字符串截成指定长度
          /// <summary>
        
/// 把字符串截成指定长度
          
/// </summary>
        
/// <param name="Content">字符串</param>
        
/// <param name="Length">需要的长度(中文的长度算2)</param>
        
/// <param name="hasAct">是否有...</param>  
        
/// <returns></returns>
        public static string CutString(string Content, int Length, bool hasAct)
        {
            if (Content == null || Content.Length == 0 || Length <= 0)
                return string.Empty;
            else if (Content.Length <= Length / 2)
                return Content;
            int l = Content.Length;

            #region 计算长度
            int clen = 0;
            while (clen < Length && clen < l)
            {
                //每遇到一个中文,则将目标长度减一。
                if ((int)Content[clen] > 128)
                    Length--;
                clen++;
            }
            #endregion
            if (hasAct)
                return Content.Substring(0, clen) + "...";
            else
                return Content.Substring(0, clen);
        }

4.判断是否是数字

判断是否是数字
        #region 判断是否是数字
        public static bool isNumeric(string str)
        {
            char[] ch = new char[str.Length];
            ch = str.ToCharArray();
            for (int i = 0; i < ch.Length; i++)
            {
                if (ch[i] < 48 || ch[i] > 57)
                    return false;
            }
            return true;

        }

        #endregion

5.截取字符

截取字符串
        /// <summary>
        
/// 截取字符串
        
/// </summary>
        
/// <param name="str">字符串</param>
        
/// <param name="chara">指定要截取的字符位置</param>
        
/// <param name="length">要截取的字符长度</param>
        
/// <returns>截取后的字符</returns>
        public static string InterceptStr(string str, string chara, int length)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return str.Remove(str.LastIndexOf(chara), length);
            }
            else
            {
                return "";
            }          
        }

6.格式化字符串

View Code
       /// <summary>
       
/// 如果字符串的值为空,则返回“暂无”
        
/// </summary>
        public static string ReturnStr(string str)
        {
            return (str == null || str.Equals(string.Empty)) ? "暂无" : str;
        }

7.获取当前页面的URL

 

View Code
        #region 获取页面地址
        /// <summary>        
        
/// 获取当前页面的URL
        
/// </summary>       
        
/// <returns></returns>    
        public static string GetUrl()
        {
            string strTemp = "http://";
            //if (System.Web.HttpContext.Current.Request.ServerVariables["HTTPS"] == "off")
            
//{ strTemp = "http://"; }
            
//else { strTemp = "https://"; }
            strTemp = strTemp + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            if (System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"] != "80")
            { strTemp = strTemp + ":" + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; }
            strTemp = strTemp + System.Web.HttpContext.Current.Request.ServerVariables["URL"];
            if (System.Web.HttpContext.Current.Request.QueryString.AllKeys.Length > 0)
            { strTemp = strTemp + "?" + System.Web.HttpContext.Current.Request.QueryString; }
            return strTemp;
        }
        #endregion

 

posted @ 2011-10-09 11:09  一只小青蛙  阅读(237)  评论(0编辑  收藏  举报