• 常用函数基类


    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    
    
    namespace HuLangCms
    {
        /// <summary>
        /// 常用函数基类
        /// </summary>
        public class FunObject
        {
            #region 替换字符串
            /// <summary>
            /// 功能:替换字符
            /// </summary>
            /// <param name="strVAlue">字符串</param>
            /// <returns>替换掉'的字符串</returns>
            public string FilterSQL(string strVAlue)
            {
                string str = "";
                str = strVAlue.Replace("''", "");
                return str;
            }
            #endregion
    
            #region 对表 表单内容进行转换HTML操作,
            /// <summary>
            /// 功能:对表 表单内容进行转换HTML操作,
            /// </summary>
            /// <param name="fString">html字符串</param>
            /// <returns></returns>
            public string HtmlCode(string fString)
            {
                string str = "";
                str = fString.Replace(">", ">");
                str = fString.Replace("<", "<");
                str = fString.Replace(" ", " ");
                str = fString.Replace("\n", "<br />");
                str = fString.Replace("\r", "<br />");
                str = fString.Replace("\r\n", "<br />");
    
                return str;
            }
            #endregion
    
            #region 判断是否:返回值:√ or ×
            /// <summary>
            /// 判断是否:返回值:√ or ×
            /// </summary>
            /// <param name="b">true 或false</param>
            /// <returns>√ or ×</returns>
            public string Judgement(bool b)
            {
                string s = "";
                if (b == true)
                    s = "<b><font color=#009900>√</font></b>";
                else
                    s = "<b><font color=#FF0000>×</font></b>";
                return s;
            }
            #endregion
    
            #region 截取字符串
            /// <summary>
            /// 功能:截取字符串长度
            /// </summary>
            /// <param name="str">要截取的字符串</param>
            /// <param name="length">字符串长度</param>
            /// <param name="flg">true:加...,flase:不加</param>
            /// <returns></returns>
            public string GetString(string str, int length, bool flg)
            {
                int i = 0, j = 0;
                foreach (char chr in str)
                {
                    if ((int)chr > 127)
                    {
                        i += 2;
                    }
                    else
                    {
                        i++;
                    }
                    if (i > length)
                    {
                        str = str.Substring(0, j);
                        if (flg)
                            str += "......";
                        break;
                    }
                    j++;
                }
                return str;
            }
            #endregion
    
            #region 截取字符串+…
            /// <summary>
            /// 格式化字符串,取字符串前 strLength 位,其他的用...代替.
            /// 计算字符串长度。汉字两个字节,字母一个字节
            /// </summary>
            /// <param name="str">字符串</param>
            /// <param name="strLength">字符串长度</param>
            /// <returns></returns>
            public string FormatStr(string str, int len)
            {
                ASCIIEncoding ascii = new ASCIIEncoding();
                int tempLen = 0;
                string tempString = "";
                byte[] s = ascii.GetBytes(str);
                for (int i = 0; i < s.Length; i++)
                {
                    if ((int)s[i] == 63)
                    { tempLen += 2; }
                    else
                    { tempLen += 1; }
                    try
                    { tempString += str.Substring(i, 1); }
                    catch
                    { break; }
                    if (tempLen > len) break;
                }
                //如果截过则加上半个省略号 
                byte[] mybyte = System.Text.Encoding.Default.GetBytes(str);
                if (mybyte.Length > len)
                    tempString += "...";
                tempString = tempString.Replace(" ", " ");
                tempString = tempString.Replace("<", "<");
                tempString = tempString.Replace(">", ">");
                tempString = tempString.Replace('\n'.ToString(), "<br>");
                return tempString;
            }
    
            /// <summary>
            /// 截取字符串+…
            /// </summary>
            /// <param name="strInput"></param>
            /// <param name="intlen"></param>
            /// <returns></returns>
            public string CutString(string strInput, int intlen)//截取字符串
            {
                ASCIIEncoding ascii = new ASCIIEncoding();
                int intLength = 0;
                string strString = "";
                byte[] s = ascii.GetBytes(strInput);
                for (int i = 0; i < s.Length; i++)
                {
                    if ((int)s[i] == 63)
                    {
                        intLength += 2;
                    }
                    else
                    {
                        intLength += 1;
                    }
    
                    try
                    {
                        strString += strInput.Substring(i, 1);
                    }
                    catch
                    {
                        break;
                    }
    
                    if (intLength > intlen)
                    {
                        break;
                    }
                }
                //如果截过则加上半个省略号
                byte[] mybyte = System.Text.Encoding.Default.GetBytes(strInput);
                if (mybyte.Length > intlen)
                {
                    strString += "…";
                }
                return strString;
            }
            #endregion
    
            #region 字符串分函数
            /// <summary>
            /// 字符串分函数
            /// </summary>
            /// <param name="strID"></param>
            /// <param name="index"></param>
            /// <param name="Separ"></param>
            /// <returns></returns>
            public string StringSplit(string strings, int index, string Separ)
            {
                string[] s = strings.Split(char.Parse(Separ));
                return s[index];
            }
            #endregion
    
            #region 分解字符串为数组
            /// <summary>
            /// 字符串分函数
            /// </summary>
            /// <param name="str">要分解的字符串</param>
            /// <param name="splitstr">分割符,可以为string类型</param>
            /// <returns>字符数组</returns>
            public string[] splitstr(string str, string splitstr)
            {
                if (splitstr != "")
                {
                    System.Collections.ArrayList c = new System.Collections.ArrayList();
                    while (true)
                    {
                        int thissplitindex = str.IndexOf(splitstr);
                        if (thissplitindex >= 0)
                        {
                            c.Add(str.Substring(0, thissplitindex));
                            str = str.Substring(thissplitindex + splitstr.Length);
                        }
                        else
                        {
                            c.Add(str);
                            break;
                        }
                    }
                    string[] d = new string[c.Count];
                    for (int i = 0; i < c.Count; i++)
                    {
                        d[i] = c[i].ToString();
                    }
                    return d;
                }
                else
                {
                    return new string[] { str };
                }
            }
            #endregion
    
            #region URL编码
            /// <summary>
            /// URL编码
            /// </summary>
            /// <param name="str">字符串</param>
            /// <returns></returns>
            public string UrlEncoding(string str)
            {
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
                return System.Text.Encoding.UTF8.GetString(bytes).ToString();
            }
            #endregion
    
    
            #region 根据传入的字符串是否为yes/no返回Bit
            /// <summary>
            /// 根据传入的字符串是否为yes/no返回Bit
            /// </summary>
            /// <param name="flg"></param>
            /// <returns></returns>
            public int GetBitBool(string flg)
            {
                int str = 0;
                switch (flg.ToLower())
                {
                    case "yes":
                        str = 1;
                        break;
                    case "no":
                        str = 0;
                        break;
                    default:
                        break;
                }
                return str;
            }
            #endregion
    
            #region Html编码
            /// <summary>
            /// HTML编码
            /// </summary>
            /// <param name="strInput"></param>
            /// <returns></returns>
            public string HtmlEncode(string strInput)
            {
                string str;
                try
                {
                    str = HttpContext.Current.Server.HtmlEncode(strInput);
                }
                catch
                {
                    str = "error";
                }
                return str;
            }
            /// <summary>
            /// HTML解码
            /// </summary>
            /// <param name="strInput"></param>
            /// <returns></returns>
            public string HtmlDecode(string strInput)
            {
                string str;
                try
                {
                    str = HttpContext.Current.Server.HtmlDecode(strInput);
                }
                catch
                {
                    str = "error";
                }
                return str;
            }
            #endregion
    
            #region 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
            /// <summary>
            /// 检测一个字符符,是否在另一个字符中,存在,存在返回true,否则返回false
            /// </summary>
            /// <param name="srcString">原始字符串</param>
            /// <param name="aimString">目标字符串</param>
            /// <returns></returns>
            public bool IsEnglish(string srcString, string aimString)
            {
                bool Rev = true;
                string chr;
                if (aimString == "" || aimString == null) return false;
                for (int i = 0; i < aimString.Length; i++)
                {
                    chr = aimString.Substring(i, 1);
                    if (srcString.IndexOf(chr) < 0)
                    {
                        return false;
                    }
    
                }
                return Rev;
            }
            #endregion
    
            #region 检测字符串中是否含有中文及中文长度
            /// <summary>
            /// 检测字符串中是否含有中文及中文长度
            /// </summary>
            /// <param name="str">要检测的字符串</param>
            /// <returns>中文字符串长度</returns>
            public int CnStringLength(string str)
            {
                ASCIIEncoding n = new ASCIIEncoding();
                byte[] b = n.GetBytes(str);
                int l = 0;  // l 为字符串之实际长度 
                for (int i = 0; i <= b.Length - 1; i++)
                {
                    if (b[i] == 63)  //判断是否为汉字或全脚符号 
                    {
                        l++;
                    }
                }
                return l;
    
            }
            #endregion
    
            #region 取字符串右侧的几个字符
            /// <summary>
            /// 取字符串右侧的几个字符
            /// </summary>
            /// <param name="str">字符串</param>
            /// <param name="length">右侧的几个字符</param>
            /// <returns></returns>
            public string GetStrRight(string str, int length)
            {
                string Rev = "";
    
                if (str.Length < length)
                {
                    Rev = str;
    
                }
                else
                {
                    Rev = str.Substring(str.Length - length, length);
                }
                return Rev;
    
    
            }
            #endregion
    
            #region 替换右侧的字符串
    
            /// <summary>
            /// 替换右侧的字符串
            /// </summary>
            /// <param name="str">字符串</param>
            /// <param name="strsrc">右侧的字符串</param>
            /// <param name="straim">要替换为的字符串</param>
            /// <returns></returns>
            public string RepStrRight(string str, string strsrc, string straim)
            {
    
                string Rev = "";
                if (GetStrRight(str, strsrc.Length) != strsrc)
                {
                    Rev = str;
                }
                else
                {
                    Rev = str.Substring(0, str.Length - strsrc.Length).ToString() + straim.ToString();
                }
                return Rev;
            }
            #endregion
        }
    
    }
    

  • 相关阅读:
    IDEA 必要配置
    人工智能能力提升指导总结
    lerna管理前端packages的最佳实践
    【和60】软件即服务的三重境界
    项目章程
    阅读计划
    使用PHP处理文本小技巧
    PHP性能分析
    Ubuntu安装nodejs和yarn
    挖矿病毒
  • 原文地址:https://www.cnblogs.com/hulang/p/1922300.html
Copyright © 2020-2023  润新知