• 工具类


    using System.Web;
    using System.Data;
    using System;
    
    /// <summary>
    ///Util 的摘要说明
    /// </summary>
    public class Util
    {
        /// <summary>
        /// 过滤字符串中的html代码
        /// </summary>
        /// <param name="Str">传入字符串</param>
        /// <returns>过滤后的字符串</returns>
        public string LostHTML(string Str)
        {
            string Re_Str = "";
            if (Str != null)
            {
                if (Str != string.Empty)
                {
                    string Pattern = "<\\/*[^<>]*>";
                    Re_Str = System.Text.RegularExpressions.Regex.Replace(Str, Pattern, "");
                }
            }
            return (Re_Str.Replace("\r", "")).Replace("\n", "").Replace("\t", "");
        }
        /// <summary>
        /// 获取指定长度字符串
        /// </summary>
        /// <param name="content">字符串内容</param>
        /// <param name="Num">字节的长度</param>
        /// <returns></returns>
        public string GetContent(object obj, int num)
        {
            string content = getToString(obj);
            if (content.Length < 1)
            {
                return "";
            }
            if (System.Text.Encoding.Default.GetBytes(content).Length > num)
            {
                int i = 0;
                string str = "";
                while (true)
                {
                    string temp = str + content.Substring(i, 1);
                    if (System.Text.Encoding.Default.GetBytes(temp).Length <= num)
                    {
                        str = temp;
                    }
                    else
                    {
                        return str;
                    }
                    i++;
                }
            }
            return content;
        }
        /// <summary>
        /// 获取指定长度字符串
        /// </summary>
        /// <param name="content">字符串内容</param>
        /// <param name="num">字节的长度</param>
        /// <param name="endstring">是否加省略字符串</param>
        /// <returns>截取后的字符串</returns>
        public string GetContent(object obj, int num, string endstring)
        {
            string content = getToString(obj);
            if (content.Length < 1)
            {
                return "";
            }
            if (endstring != null && endstring.Length > 0)
            {
                if (System.Text.Encoding.Default.GetBytes(content).Length > num)
                {
                    return string.Format("{0}{1}", GetContent(content, num), endstring);
                }
            }
            return GetContent(content, num);
        }
        public string getJieSheng(object old, object cur)
        {
            return (getToDecimal(old) - getToDecimal(cur)).ToString("f2");
        }
    
        public bool getToBool(object obj)
        {
            if (obj == null)
                return false;
            bool flag = false;
            bool.TryParse(obj.ToString(), out flag);
            return flag;
        }
        public int getToInt(object obj)
        {
            if (obj == null)
                return 0;
            int def;
            int.TryParse(obj.ToString(), out def);
            return def;
        }
        public decimal getToDecimal(object obj)
        {
            decimal def = 0;
            if (obj == null)
                return def;
            decimal.TryParse(obj.ToString(), out def);
            return def;
        }
        public int getToInt(object obj, int def)
        {
            if (obj == null)
                return def;
            int.TryParse(obj.ToString(), out def);
            return def;
        }
        public string getToString(object obj)
        {
            if (obj == null)
                return "";
            string aa = obj.ToString().Trim();
            if (aa.Length < 1)
                return "";
            return aa;
        }
        public string getToString(object obj, string def)
        {
            if (obj == null)
                return def;
            string aa = obj.ToString().Trim();
            if (aa.Length < 1)
                return def;
            return aa;
        }
        HttpRequest request = HttpContext.Current.Request;
        public int getFormInt(string obj)
        {
            return getToInt(request.Form[obj]);
        }
        public string getFormString(string obj)
        {
            return getToString(request.Form[obj]);
        }
        public int getQueryStringInt(string obj)
        {
            return getToInt(request.QueryString[obj]);
        }
        public string getQueryStringString(string obj)
        {
            return getToString(request.QueryString[obj]);
        }
        public long getQueryStringLong(string obj)
        {
            return getToLong(request.QueryString[obj]);
        }
        public long getToLong(object obj)
        {
            long def = 0;
            if (obj == null)
                return def;
            long.TryParse(obj.ToString(), out def);
            return def;
        }
    
        public string getBanner(DataRow[] drs, int width, int height, string tag, int len)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (drs.Length > 0)
            {
                bool _target, _flag = tag.Length > 0 ? true : false;
                string _href, _title, _alt;
                int i = 0;
                foreach (DataRow dr in drs)
                {
                    if (len == i && len != 0) { break; }
                    i++;
                    _href = getToString(dr["GetUrl"]);
                    _title = getToString(dr["Title"]);
                    _target = getToBool(dr["NewWindow"]);
                    _alt = getToString(dr["Alt"]);
                    if (_flag) { sb.AppendFormat("<{0}>", tag); }
                    sb.AppendFormat("<a href='{0}'{1}{2}><img src='{3}'{5}{6}{4} /></a>",
                    _href.Length > 0 ? _href : "#none", _title.Length > 0 ? " title='" + _title + "'" : "",
                    _target ? " target='_blank'" : "", dr["Image"], _alt.Length > 0 ? " alt='" + _alt + "'" : "",
                    width > 0 ? " width='" + width + "'" : "", height > 0 ? " height='" + height + "'" : "");
                    if (_flag) { sb.AppendFormat("</{0}>", tag); }
                }
            }
            return sb.ToString();
        }
        public string getBanner(DataRow[] drs, int width, int height, string tag, int len, string css)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (drs.Length > 0)
            {
                bool _target, _flag = tag.Length > 0 ? true : false;
                string _href, _title, _alt;
                int i = 0;
                foreach (DataRow dr in drs)
                {
                    if (len == i && len != 0) { break; }
                    i++;
                    _href = getToString(dr["GetUrl"]);
                    _title = getToString(dr["Title"]);
                    _target = getToBool(dr["NewWindow"]);
                    _alt = getToString(dr["Alt"]);
                    if (_flag) { sb.AppendFormat("<{0}{1}>", tag, css); }
                    sb.AppendFormat("<a href='{0}'{1}{2}><img src='{3}'{5}{6}{4} /></a>",
                    _href.Length > 0 ? _href : "#none", _title.Length > 0 ? " title='" + _title + "'" : "",
                    _target ? " target='_blank'" : "", dr["Image"], _alt.Length > 0 ? " alt='" + _alt + "'" : "",
                    width > 0 ? " width='" + width + "'" : "", height > 0 ? " height='" + height + "'" : "");
                    if (_flag) { sb.AppendFormat("</{0}>", tag); }
                }
            }
            return sb.ToString();
        }
        public string getMatchProductImg(object obj, string size)
        {
            string pic = getToString(obj);
            if (pic.Length > 0)
            {
                return pic.Replace("/UploadImage/Product/", "/UploadImage/Product/Thumbnail/" + size + "/");
            }
            return "/images/nopic.jpg";
        }
        /// <summary>
        /// 获得分类的上级
        /// </summary>
        /// <param name="classid">当前分类编号</param>
        /// <returns>详细分类</returns>
        public string getParentStr(object classid)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            DataTable dt = new MySqlExecute().getDT(string.Format(@"with my as
    (
    select ClassID,ClassName,ParentID from TB_Category where ClassID={0}
    union all
    select aa.ClassID,aa.ClassName,aa.ParentID from my,TB_Category aa where my.ParentID=aa.ClassID and aa.ParentID<>0
    ) select * from my", classid));
            int len = dt.Rows.Count;
            if (len > 0)
            {
                for (int i = len - 1; i >= 0; i--)
                {
                    sb.Append("<span>></span>");
                    if (getToLong(dt.Rows[i]["ClassID"]) == getToLong(classid))
                    {
                        sb.AppendFormat("<strong>{0}</strong>", dt.Rows[i]["ClassName"]);
                    }
                    else
                    {
                        sb.AppendFormat("<a href='/product/?classid={0}'>{1}</a>", dt.Rows[i]["ClassID"], dt.Rows[i]["ClassName"]);
                    }
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 获得地区的上级
        /// </summary>
        /// <param name="classid">当前地区编号</param>
        /// <returns>详细地区</returns>
        public string getAddressStr(object classid)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            DataTable dt = new MySqlExecute().getDT(string.Format(@"with my as
    (
    select AreaID,AreaName,ParentID from TB_Area where AreaID={0}
    union all
    select aa.AreaID,aa.AreaName,aa.ParentID from my,TB_Area aa where my.ParentID=aa.AreaID and aa.ParentID<>0
    ) select * from my", classid));
            int len = dt.Rows.Count;
            if (len > 0)
            {
                for (int i = len - 1; i >= 0; i--)
                {
                    sb.AppendFormat("{0}", dt.Rows[i]["AreaName"]);
                }
            }
            return sb.ToString();
        }
        public string getToDateStr(object obj)
        {
            return Convert.ToDateTime(obj).ToString("MM/dd/yyyy HH:mm:ss").Replace("-", "/");
        }
        public string getToBrStr(object obj)
        {
            return getToString(obj);//.Replace("\r\n", "<br />")
        }
        public string getSqlClear(string aa)
        {
            return aa.Replace("'", "''").Replace("[-|;|@|%]", "");
        }
    }
  • 相关阅读:
    使用linux将一个服务器上的文件或者文件夹复制黏贴到另一个服务器上
    深度学习之常用linux命令总结
    python 操作MYSQL数据库
    MYSQL登录及常用命令
    mysql数据库可视化工具—Navicat Premium—安装与激活
    Mysql数据库的简单介绍与入门
    java 正则匹配int型
    js设置文本框只能输入数字
    Java数据抓取(一)
    Nodejs的多线程
  • 原文地址:https://www.cnblogs.com/daixingqing/p/2768443.html
Copyright © 2020-2023  润新知