C#: How to eliminate whitespaces in a string...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saravanan Krishnan
    New Member
    • Mar 2008
    • 11

    C#: How to eliminate whitespaces in a string...

    Hi, i'm writting a windows application in C# the functionality that i'm trying is to implement is to add items to a combo box at run time & when i select one item, my app should filter the list contents based on the selection i made in the combo box.

    I'm able to add items to combo box and also filter the list contents. But the only trouble i'm facing is when i select a item with a whitespace in the middle of a string in the conbo box, it does not filter the records and displays nothing in the DGV.

    I'm in serious urgency, any help is appreciable...

    Thanks in Advance...

    Saravanan Krishnan
  • davidson1
    New Member
    • Feb 2008
    • 144

    #2
    use Trim() fuction which elimates whitespace...

    Davidson

    10 March 2008

    Comment

    • Saravanan Krishnan
      New Member
      • Mar 2008
      • 11

      #3
      <<
      Originally posted by davidson1
      use Trim() fuction which elimates whitespace...

      Davidson

      10 March 2008
      >>posted davidson

      I've used List<T> where T is a class with various properties and added values to those properties vItemList is the instance of List<T> now i'm filtering the Items Property and based on the combo box selection...
      [Heres the code snippet]

      string str;
      str = cmbitem.selecte dtext;
      str = str.replace(" ","");
      //i tried str = str.trim();
      //Even i tried str = str.replace(" ",string.empty) ;
      vItemList.Filte r = "Items=" + str;


      This works(filters) fine when a string like "LAPTOP" is selected from the combobox and the filtered records are displayed in the dgv but it doesn't display anything when a string like "Notebook PC" is selected...

      Now say me where am i making mistake....

      Thanks...

      Saravanan Krishnan

      Comment

      • tagg3rx
        New Member
        • Jun 2007
        • 35

        #4
        I Think the following Code Snip it will give you what your looking for.

        Code:
                string _str = "Laptop PC";
                List<string> _vals = new List<string>{"Computer", "Monitor", "Laptop PC"};
                List<string> _result = new List<string>();
                foreach (string _val in _vals)
                    if (_val.Contains(_str))
                        _result.Add(_val);

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          The line:
          str = str.replace(" ","");
          Should have been removing the whitespace correctly, did you set a breakpoint and look to see what it produces?
          Is it possible that that is NOT a whitespace in there?

          Can you filter on a name that should have a space and then doesn't?
          I'm not realy clear on what you're up to.

          Comment

          • Saravanan Krishnan
            New Member
            • Mar 2008
            • 11

            #6
            Using this line
            vItemList.Filte r = "Items='" + str + "'";

            Instead of
            vItemList.Filte r = "Items=" + str;

            this code helped me out, if someone's out for this use this...

            Thanking all for spending your time helping me...

            Saravanan Krishnan

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Ahh ok, use single quotes around the value.
              i.e. Items='Notebook PC'

              Comment

              • Saravanan Krishnan
                New Member
                • Mar 2008
                • 11

                #8
                By using the code snippet which i posted in my last reply works absolutely fine, the itemlist filters the records perfectly while even the string with a whitespace is selected [like "Notebook PC"].

                Similarly i have multiple combo boxes in my form and i also want to filter the records in my list based on multiple combo boxes selections as well as single combo box (it's left to users choice), now how do i identify that the user has selected one or multiple combo boxes and based on the selections the list has to filter...

                I thought i would use switch case to identify the selected number of combo boxes, but i dont know how to give the switch expression. Or else is there any alternate way to do that...


                Thanks in advance...

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  If a user has not made a selection in a combo box, the selected index should be -1 yes? Could you just check them all to see if a valid combobox index is selected?
                  (Note, if it's not -1, you can always have a first entry say "Do Not Filter" or something and know to ignore index of 0 as well)

                  Comment

                  • tagg3rx
                    New Member
                    • Jun 2007
                    • 35

                    #10
                    Don't forget to add autopostback to the combo boxes or you wont be able to perform any action when the user checks a box

                    Comment

                    Working...