日期:2014-05-19  浏览次数:20871 次

取子字符串时,把分隔符也取出来,该怎么做?
我现在的代码是这样写的:
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 '
也就是说,输入多少%,都是以%为分隔符,且提取的子字符串前后都有%的话要包含%,位置也应该一样
各位大大,谢了!

------解决方案--------------------
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;