Searching words seperated by Space in another string seperated with Comma

bahare
 
on Dec 28, 2012 09:51 AM
3141 Views

Hi, I have a field in database that a string with seperator stored in it something like this flower,tree,leaf

how can I don't let admin add duplicate words to this string when he add new words like this by entering these words in a textbox?

greentree,tree,browntrees

for exmaple I dont want

flower,tree,leaf,tree,tree

I think I must do it by linq but I don't know how?

thanks.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Dec 28, 2012 09:52 AM

Where actually you want to search? Within string or database?

 

bahare
 
on Dec 28, 2012 11:25 AM

I don't get your mean

I have a field in database and I want to don't let admin add duplicate keyword to it nevermind by which way.

I wrote this code but no success

 

 

 public string searchkeywords()
    {
        string stringintable = "flower,tree,leaf";
        string newstring = "greentree,tree,browntree";
        string[] myarray = newstring.Split(',');
        string[] ddddd = stringintable.Split(',');
        string mylaststring = "";
        for (int i = 0; i < myarray.Length; i++)
        {
            string t = myarray[i];

            var result = from n in ddddd
                         where n == t
                         select n;
            if (result != null)
            {
                mylaststring += "," + result.ToString();
            }
        }
        return mylaststring;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        string ddd = searchkeywords();
        Response.Write(ddd);
    }
}

that is for test

in reality I will write somthing like this:

 SqlDataReader dr = MyDALBase.ExecuteReader(System.Data.CommandType.StoredProcedure, "editkewords", new SqlParameter[]{
          new SqlParameter("@CategoryId",CategoryId)
        });
        dr.Read();

string stringintable = dr["keywords"].tostring();
        string newstring = textbox1.text
bahare
 
on Dec 28, 2012 01:58 PM

the problem solved.

this is code:

 

    public List<string> searchkeywords()
    {
        string string1 = "flower,tree,leaf";
        string[] arr1 = string1.Split(',');
        string string2 = "greentree,tree,browntree";
        string[] arr2 = string2.Split(',');
        
        List<string> myList = new List<string>();
        myList.Add(string1);
        for (int i = 0; i < arr2.Length; i++)
        {
            string t = arr2[i].ToString();

            if (!StringUtils.Exists(t, arr1))
            {
                myList.Add(t);
            }
             
        }
        return myList;
    }


    public static class StringUtils
{
 public static bool Exists(string aString, params string[] aStringsList)
 {
   foreach (string listString in aStringsList)
    if (aString == listString)
     return true;
   return false;
 }
}

    protected void Button5_Click(object sender, EventArgs e)
    {
        List<string> mylist = searchkeywords();
        string ddd=string.Join(",",mylist);
        Response.Write(ddd);
    }

if there is a better way please let me know!