• [原创]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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    关于Qt国产化系统开发的几点总结
    C#和Qt实现的对于异常处理的一个使用策略
    echarts 自定义省份区块颜色的两种方法
    获取某个文件下的.vue文件
    仿写单选框/多选框
    重置对象属性
    elementUI之树形数据Table遇到的问题
    sass使用记录
    koa的使用记录
    六顶思想帽
  • 原文地址:https://www.cnblogs.com/chhuic/p/1691621.html
Copyright © 2020-2023  润新知