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

原文地址:https://www.cnblogs.com/chhuic/p/1691621.html