日期:2014-05-18  浏览次数:20850 次

一个问题很有意思,大家看看怎么解决~~
有两个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

------解决方案--------------------
mark

------解决方案--------------------
这不就是一个字符串的查找吗

------解决方案--------------------
不难,就是麻烦

C# code
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;
}

------解决方案--------------------
路过