locked
How to detect a word from a string in a listbox? RRS feed

  • Question

  • Hi all,

    How do i detect a certain word from a string in my listbox?

    For example the strings maybe:

    " Open picture gallery."

    "Please open gallery"

    and i want to detect the word 'open'.

    My string will be different each time so how do i detect only the word "open"?

    Thanks in advance

    Wednesday, January 16, 2013 9:15 AM

Answers

  • As you said, use Contains...

    i.e.

    if(inString.ToLower().Contains("open"))
        DoTheStuff();


    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |


    • Edited by VendorX Wednesday, January 16, 2013 9:39 AM
    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:03 AM
    • Marked as answer by jojoleo Thursday, January 17, 2013 3:01 AM
    Wednesday, January 16, 2013 9:38 AM
  • Thanks Venkat786,

    It is almost there! However the message will open up thrice if there are 3 items in my listbox because it's searching through all items.

    But i  just want it to detect the latest item that was being sent and show up the messagebox.

    Here's the code.

      string nom;
    
                nom = txtBox.Text;
                lstItems.Items.Add(nom);
                string yoursearchstring = "open";
    
                for (int i = 0; i < lstItems.Items.Count; ++i)
                {
                    if (lstItems.Items[i].ToString().Contains(yoursearchstring))
                    {
                        MessageBox.Show("Opening in progress...");
                    }
                    else
                    {
                        MessageBox.Show("'OPEN' not detected!");
                    }
                }

    • Marked as answer by jojoleo Thursday, January 17, 2013 3:22 AM
    Thursday, January 17, 2013 2:18 AM
  •             string nom;
    
                nom = txtBox.Text;
                lstItems.Items.Add(nom);
                string yoursearchstring = "open";
    
                // method 1 
    
                int i = lstItems.Items.Count;
                int p = i - 1;
    
                if (lstItems.Items[p].ToString().Contains(yoursearchstring))
                {
                    MessageBox.Show("Opening in progress...");
                }
                else
                {
                    MessageBox.Show("'OPEN' not detected!");
                }
    
                // method 2
    
                if (nom.Contains("open"))
                {
                    MessageBox.Show("Opening in progress...");
                }
                else
                {
                   MessageBox.Show("'OPEN' not detected!");
                }

    Thank you guys :)
    • Marked as answer by jojoleo Thursday, January 17, 2013 3:13 AM
    Thursday, January 17, 2013 3:12 AM

All replies

  • As you said, use Contains...

    i.e.

    if(inString.ToLower().Contains("open"))
        DoTheStuff();


    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |


    • Edited by VendorX Wednesday, January 16, 2013 9:39 AM
    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:03 AM
    • Marked as answer by jojoleo Thursday, January 17, 2013 3:01 AM
    Wednesday, January 16, 2013 9:38 AM
  • Hi ioioleo,

    you can use indexof method of string.

    From MSDN :

    indexof Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance.

    once you get your index,you can use any other methods to populate your string.

     string myText = "I love the world of programming";
                int positionOfword = myText.IndexOf("world");

    EDIT : personally I prefer Indexod method more, because string.contains method return only true or false, but If you found the string(i.e. string.contains method returns true) in most of case you have to found index for string population.

    may be it helps you.

    If anything is unclear feel free to ask me... :)

    Thanks,

    Nans11



    ENjoy ThE WorLD Of COdE


    • Edited by Nans11 Wednesday, January 16, 2013 10:02 AM justification of IndexOf Method
    • Proposed as answer by Cor Ligthert Wednesday, January 16, 2013 10:04 AM
    Wednesday, January 16, 2013 9:43 AM
  • Thanks Nans11 for your quick response,

    maybe i should rephrase,

    How should i detect the word if the word is in an item of a listbox?

    Any clue?

    Thank you in advance :)

    Cheers!

    Thursday, January 17, 2013 1:18 AM
  • JoJo Leo :

    You need to loop through listbox items .. May be this is of a start

      string[] arr = { "Open picture gallery.", "Please open gallery" };
                listBox1.Items.AddRange(arr);
                
                string yoursearchstring = "Open";
    
                for (int i = 0; i < listBox1.Items.Count; ++i)
                {
                    if (listBox1.Items[i].ToString().Contains(yoursearchstring))
                    {
                        //Found your string do your stuff
                    }
                }
    

    Thursday, January 17, 2013 1:37 AM
  • Thursday, January 17, 2013 1:48 AM
  • Thanks Venkat786,

    It is almost there! However the message will open up thrice if there are 3 items in my listbox because it's searching through all items.

    But i  just want it to detect the latest item that was being sent and show up the messagebox.

    Here's the code.

      string nom;
    
                nom = txtBox.Text;
                lstItems.Items.Add(nom);
                string yoursearchstring = "open";
    
                for (int i = 0; i < lstItems.Items.Count; ++i)
                {
                    if (lstItems.Items[i].ToString().Contains(yoursearchstring))
                    {
                        MessageBox.Show("Opening in progress...");
                    }
                    else
                    {
                        MessageBox.Show("'OPEN' not detected!");
                    }
                }

    • Marked as answer by jojoleo Thursday, January 17, 2013 3:22 AM
    Thursday, January 17, 2013 2:18 AM
  • You don't need iterate, just use sample from my first post - only replace inString by nom...

    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |

    Thursday, January 17, 2013 2:34 AM
  • Yup got it as well. Thanks!

    I thought what was thinking what is an Instring.

    Thursday, January 17, 2013 2:57 AM
  •             string nom;
    
                nom = txtBox.Text;
                lstItems.Items.Add(nom);
                string yoursearchstring = "open";
    
                // method 1 
    
                int i = lstItems.Items.Count;
                int p = i - 1;
    
                if (lstItems.Items[p].ToString().Contains(yoursearchstring))
                {
                    MessageBox.Show("Opening in progress...");
                }
                else
                {
                    MessageBox.Show("'OPEN' not detected!");
                }
    
                // method 2
    
                if (nom.Contains("open"))
                {
                    MessageBox.Show("Opening in progress...");
                }
                else
                {
                   MessageBox.Show("'OPEN' not detected!");
                }

    Thank you guys :)
    • Marked as answer by jojoleo Thursday, January 17, 2013 3:13 AM
    Thursday, January 17, 2013 3:12 AM