• [原创]DataView特殊字条串替换(过滤)


         

                  string sSql = "";

                    string sValue = txtCarNo.Text.Trim();

                    // 查询内容未输入

                    if (sValue.Length == 0)
                    {
                        txtCarNo.Focus();
                        return;
                    }
                    sValue = PublicClass.Check.ValueReplace(sValue);//特殊字条串处理
                    if (comType.SelectedValue.ToString() == "0")
                    {
                        // 车牌号码
                        sSql = "CarNum like '%" + sValue + "%'";
                    }

              DataView dv = new DataView(m_dtCarList, sSql, "AreaCode,CarNum", DataViewRowState.CurrentRows);

    #region  特殊字条串替换(DataView过滤)
            /// <summary>
            /// 特殊字条串替换(DataView过滤)
            /// </summary>
            /// <param name="sValue">原始字符串</param>
            /// <returns>新字符串</returns>
            public static string ValueReplace(string sValue)
            {
                string tempValue = "";
                try
                {
                    string[] sList = sValue.Split('[');
                    int index = sValue.IndexOf('[');

                    for (int i = 0; i < sList.Length; i++)
                    {
                        sList[i] = sList[i].Replace("]", "[]]");
                        sList[i] = sList[i].Replace("~", "[~]");
                        sList[i] = sList[i].Replace("@", "[@]");
                        sList[i] = sList[i].Replace("^", "[^]");
                        sList[i] = sList[i].Replace("&", "[&]");
                        sList[i] = sList[i].Replace("*", "[*]");
                        sList[i] = sList[i].Replace("(", "[(]");
                        sList[i] = sList[i].Replace(")", "[)]");
                        sList[i] = sList[i].Replace(">", "[>]");
                        sList[i] = sList[i].Replace("<", "[<]");
                        sList[i] = sList[i].Replace("`", "[`]");
                        sList[i] = sList[i].Replace("-", "[-]");
                        sList[i] = sList[i].Replace("=", "[=]");

                        if (index == -1)
                        {
                            tempValue += sList[i];
                            continue;
                        }

                        if (i == 0 && index == 0)
                        {
                            tempValue += "[[]" + sList[i];
                        }
                        else if (index == sValue.Length - 1)
                        {
                            if (i == sList.Length - 1)
                            {
                                tempValue += sList[i] + "[[]";
                            }
                        }
                        else
                        {
                            if (i == sList.Length - 1)
                            {
                                tempValue += sList[i];
                            }
                            else
                            {
                                tempValue += sList[i] + "[[]";
                            }
                        }
                    }
                }
                catch { }

                return tempValue;
            }
            #endregion

    作者:chhuic

    出处:http://chhuic.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    多线程上传和下载大文件(当文件服务器对上传文件大小有限制)
    http和https的区别?
    将数据导出到 excel ,然后下载下来
    注解@Transactional(rollbackFor = Exception.class) 的用法
    浅谈 MongoDB
    浅谈负载均衡
    join,left join,inner join,full join的区别?
    Bootstrap如何适配移动浏览器
    常用正则表达式
    js获取节点
  • 原文地址:https://www.cnblogs.com/chhuic/p/1691621.html
Copyright © 2020-2023  润新知