码迷,mamicode.com
首页 > 其他好文 > 详细

2. commTools类

时间:2018-05-28 18:01:38      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:函数   gen   替代   长度   数据   string   man   mil   空值   

//去除字符串最后一个字符(主要去除后面的拼接符号);返回的是处理之后的字符串。

//<param name="str">字符串值</param>

//str.Substring()这是一个截取字符串的函数

public static string removeLastChar(string str)

        {

            if (str.Length > 0)

                return str.Substring(0, str.Length - 1);

            else

                return str;

        }

//判断字符串中是否有子串,有则返回子串前边的串,否则返回原串;

// <param name="Str">母串</param>

// <param name="subStr">子串</param>

//IndexOf()可返回某个指定的字符串值在字符串中首次出现的位置。

public static string getStrPrefix(string Str, string subStr)

        {

            int pos = Str.IndexOf(subStr);

            if (pos >= 0)

            {

                return Str.Substring(0, pos);

            }

            return Str;

        }

 //判断字符串中是否有子串,有则返回字串后边的串,否则返回原串;

// <param name="Str">母串</param>

// <param name="subStr">子串</param>

public static string getStrSuffix(string Str, string subStr)

        {

            int pos = Str.IndexOf(subStr);

            if (pos >= 0)

            {

                return Str.Substring(pos + subStr.Length);

            }

            return Str;

        }

//返回Request中指定key的值

// <param name="key">键值</param>

//<param name="isShowError">没有是否显示错误</param>

//<param name="errorMsg">错误消息</param>

//后台检测  isShowError 是否为 true,为 true则  errorMsg 显示出错信息。

 public static string getRequestValue(string key, bool isShowError = true, string errorMsg = "要获取的参数不存在")

        {

            if (HttpContext.Current.Request[key] != null)

            {

                string value = HttpContext.Current.Request[key].ToString();

                //Value进行安全监测,防止攻击

                if (key == "id")

                {

                    if (rui.typeHelper.toLong(value) == 0)

                        rui.commTools.throwException(errorMsg);

                }

 

                return value;

            }

            else

            {

                if (isShowError)

                    rui.commTools.throwException(errorMsg);

                return "";

            }

        }

 //获取表单数组的值,为空的也获取(出现同名表单时用);最后返回的值集合(包含空值);

 //<param name="name">表单name属性</param>

 public static List<string> getRequestList(string name)

        {

            //request接受客户端从表单提交的数据

            if (rui.commTools.getContext().Request[name] != null)

            {

                //返回加逗号的字符串数组需要写方法分离把他转换成List

                string value = rui.commTools.getContext().Request[name];

    //将前台取到带“,”的数组拆分,并存入List

                List<string> list = (from a in value.Split(‘,‘)

                                     select a).ToList<string>();

                return list;

            }

            else

            {

                return new List<string>();

            }

        }

 //获取客户端请求中包含的所有Key列表;

//<param name="entryPreTag">key前缀过滤</param>

 public static List<string> getRequestAllKey(string entryPreTag)

        {

            List<string> list = new List<string>();

            string[] queryString = HttpContext.Current.Request.QueryString.AllKeys;

            string[] form = HttpContext.Current.Request.Form.AllKeys;

            foreach (string item in queryString)

                list.Add(item);

            foreach (string item in form)

                list.Add(item);

            //获取与实体相关的表单Key

            if (entryPreTag != "")

            {

                list = (from a in list

                        where a.StartsWith(entryPreTag)

                        select a.Substring(entryPreTag.Length + 1)).ToList<string>();

            }

            return list;

        }

//编码累加,用于表主键字段值累加生成;返回累加的值。

//<param name="code">编码前缀</param>
//<param name="NumLength">数字位数</param>

 public static string codeGenerate(string code, int NumLength)

        {

            int value = Convert.ToInt32(code.Substring(code.Length - NumLength));

            return (++value).ToString("D" + NumLength.ToString());

        }

 /// 编码累减,用于表主键字段值累加生成

        /// <param name="code">编码前缀</param>

        /// <param name="NumLength">数字位数</param>

        /// <returns>返回的累减值</returns>

        public static string codeReduce(string code, int NumLength)

        {

            int value = Convert.ToInt32(code.Substring(code.Length - NumLength));

            return (--value).ToString("D" + NumLength.ToString());

        }

 

        /// 抛出异常

        /// <param name="msg">异常小心</param>

        public static void throwException(string msg)

        {

            ApplicationException ex = new ApplicationException(msg);

            throw ex;

        }

 

        /// 取字串

        /// <param name="value">母串</param>

        /// <param name="length">子串长度</param>

        /// <returns>返回子串</returns>

        public static string subString(object value, int length)

        {

            if (value.ToString().Length > length)

                return value.ToString().Substring(0, length) + "...";

            else

                return value.ToString();

        }

 

        /// 获取当前网页上下文

        public static HttpContext getContext()

        {

            return HttpContext.Current;

        }

 

        /// 获取请求基类

        public static HttpRequestBase getRequestBase()

        {

            System.Web.HttpRequestBase requestBase = new HttpRequestWrapper(HttpContext.Current.Request);

            return requestBase;

        }

 

        /// 获取一个新的Guid字符串;生成一个随机全球唯一的字符串,并用空字符替代其中的 - 号

        public static string getGuid()

        {

            string value = Guid.NewGuid().ToString();

            return value.Replace("-", "");

        }

 

        /// 判断是否人保存的(自动保存的会附加上auto参数,没有则人工保存的)

        /// <returns>TRUE人工保存</returns>

        public static bool isClickSave()

        {

            string auto = rui.commTools.getRequestValue("auto", false);

            return rui.typeHelper.isNullOrEmpty(auto);

        }

 

        //判断是否第一次进入

        public static bool isFirstEnter()

        {

            if (HttpContext.Current.Request["PageIndex"] == null)

                return true;

            else

                return false;

        }

 

        //处理第一次进入列表界面

        public static void handleFirstEnter(ref string querySql)

        {

            if (isFirstEnter())

                querySql += " and 1!=1 ";

        }

 

        //判断是否搜索操作(第一次进入和单击搜索都认为是搜索操作) - 区分分页操作

        public static bool isSearchOp()

        {

            if (HttpContext.Current.Request["exeCountSql"] == null || HttpContext.Current.Request["exeCountSql"].ToString() == "true")

                return true;

            return false;

        }

 

2. commTools类

标签:函数   gen   替代   长度   数据   string   man   mil   空值   

原文地址:https://www.cnblogs.com/feihusurfer/p/9101309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!