我现在的代码是这样写的:
string delimStr = "%";
char [] delimiter = delimStr.ToCharArray();
string words = textBox2.Text;
string [] split = null;
split = words.Split('%');
string strwhere ="par_desc like ";
string strsql="";
for (int x = 0; x < split.Length; x++) 
{
     if (split[x].Length > 0)
{
   if (strsql.Length >0)
     {
                  strsql += " or ";
      }
  strsql += strwhere + "'" + split[x] + "'";
}
}
label1.Text =strsql;
/////////////////////////
textBox2.Text = 1234%eafa%9865
现在提取的结果是:par_desc like '1234' or par_desc like 'eafa' or par_desc like '9865'
现在想要的结果是:par_desc like '1234%' or par_desc like '%eafa%' or par_desc like '9865'
也就是说,输入多少%,都是以%为分隔符,且提取的子字符串前后都有%的话要包含%,位置也应该一样
各位大大,谢了!

解决方案 »

  1.   

    现在想要的结果是:par_desc like '1234%' or par_desc like '%eafa%' or par_desc like '%9865'
      

  2.   

    string delimStr = "%";
    char[] delimiter = delimStr.ToCharArray();
    string words = textBox2.Text;
    string[] split = null;
    split = words.Split('%');
    string strwhere = "par_desc like ";
    string strsql = "";
    for (int x = 0; x < split.Length; x++)
    {
    if (split[x].Length > 0)
    {
    if (strsql.Length > 0)
    {
    strsql += " or ";
    }
    if (x == 0)
    {
    strsql += strwhere + "'" + split[x] + "%'";
    }
    else if (x == split.Length - 1)
    {
    strsql += strwhere + "'%" + split[x] + "'";
    }
    else
    {
    strsql += strwhere + "'%" + split[x] + "%'";
    }
    }
    }
    label1.Text = strsql;
      

  3.   

    string strwhere = "par_desc like ";
    string strsql = "";
    string words = textBox2.Text;foreach (string s in words.Split(new char[] { '%' }, 
        StringSplitOptions.RemoveEmptyEntries))
        strsql += " or " + strwhere + "'%" + s.Replace("'", "''") + "%'";
    if (strsql.Length > 0)
    {
        strsql = strsql.Remove(0, 4); // 删除 " or "
        strsql = strsql.Remove(strwhere.Length + 1, 1); // 删除 第一个"%"
        strsql = strsql.Remove(strsql.Length - 2, 1); // 删除 最后一个"%"

    label1.Text = strsql;