拆分文本形式文本框并将其丢弃为不同的字符串

时间:2014-09-04 09:32:43

标签: c# asp.net

我有一个文本框,我把文本放在下面

如果文字为Man,shirt,:businessman,-girl

然后结果就是

string Keyword = Man;
string or =shirt;
string And =businessman;
string not =girl

如果文字为Man,shirt,button,:businessman,-girl,-smile

然后结果就是

  string Keyword = Man;
    string or =shirt,button;
    string And =businessman;
    string not =girl,smile;

我想要拆分 -

 ',' to or
 ':' to and 
 '-' to not

任何人都可以解决这个问题。

1 个答案:

答案 0 :(得分:1)

试试这个。

    string text = yourtextbox.Text;
    string[] keyword = text.Split(':');
    string[] manArr = keyword[0].Split(',');

    string man = manArr[0];
    string or = "";
    for (int i = 1; i < manArr.Length; i++)
    {
        if (manArr[i].Trim() != "")
            or += manArr[i] + ",";
    }
    or = or.TrimEnd(',');
    string []andArr = keyword[1].Split('-');
    string and = andArr[0];
    string not = "";
    for (int i = 1; i < andArr.Length; i++)
    {
        not += andArr[i] + ",";
    }
    not = not.TrimEnd(',');