有两个textbox,还有一个button。要求写一个类:TextFind
包含一个方法:public bool find(string keywords,string text)
-----------------------------------------------------------
结果:
如果keywords包含在text中,则返回true,否则false。
-----------------------------------------------------------
keywords 可以用 + 或者 - 号进行多个单词的组合。
譬如:abc+123,就是说text中如包含abc 也要包含123的话,则返回true
bbc+fff+fff 同理abc+123-fff,就是说text中 如包含abc,也包含123,但是不包含fff的话,则返回true。复杂的同理: abcdef+123fdf+343-fff-3343

解决方案 »

  1.   

    不难,就是麻烦public bool find(string keywords, string text)
    {
        List<string> incList = new List<string>();
        List<string> excList = new List<string>();
        if (keywords.IndexOf("-") > -1)
        {
            string incKeys = keywords.Substring(0, keywords.IndexOf("-"));
            string excKeys = keywords.Substring(keywords.IndexOf("-") + 1);
            if (keywords.IndexOf("+") > -1)
            {
                if (incKeys.IndexOf("+") > -1)
                    incList = new List<string>(incKeys.Split('+'));
                else
                    incList.Add(incKeys);
            }
            if (excKeys.IndexOf("-") > -1)
                excList = new List<string>(excKeys.Split('-'));
            else
                excList.Add(excKeys);
        }
        else
        {
            if (keywords.IndexOf("+") > -1)
                incList = new List<string>(keywords.Split('+'));
            else
                incList.Add(keywords);
        }    if (incList.Count > 0)
        {
            foreach (string s in incList)
            {
                if (text.IndexOf(s) == -1)
                    return false;
            }
        }    if (excList.Count > 0)
        {
            foreach (string s in excList)
            {
                if (text.IndexOf(s) > -1)
                    return false;
            }
        }
        return true;
    }这里假定+和-同时出现,+在左,-在右,且是连续的,如果不是,上面的实现方法还需要改下
      

  2.   

    唉,我今天去机试,没做出来,晚上回家古道了半天,才出来。太菜了看来,唉。 public class TextFind
    {
    ArrayList Terms = new ArrayList();
    int matchSuccessfulCount = 0;
    string _content; public TextFind(string keywords,string content)
    {
    _content = content;
    parseKeywords(keywords);
    }

    void parseKeywords(string keywords)
    {
    if (!keywords.StartsWith("+") && !keywords.StartsWith("-"))
    keywords = keywords.Insert(0, "+");  //if start is null with prefix add "+" string temp = string.Empty; for (int i = 0; i < keywords.Length; i++)
    {
    if (keywords[i] == '+' || keywords[i] == '-')
    {
    if (temp.Length != 0)
    Terms.Add(temp);
    temp = string.Empty;
    } temp += keywords[i]; if (i == keywords.Length - 1)
    {
    if (temp.Length != 0)
    Terms.Add(temp);
    }
    }
    }

    bool matchTerm(string term)
    {
    if(term.StartsWith("+")) //+ is find
    return _content.IndexOf(term.Substring(1)) != -1;
    else if (term.StartsWith("-")) // - is not find
    return _content.IndexOf(term.Substring(1)) == -1;
    return false;
    } public bool Find ()
    {
    foreach ( object term in Terms)
    {
    if(matchTerm((string)term))
    matchSuccessfulCount++;
    }
    return matchSuccessfulCount == Terms.Count;
    }
    }