"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Thursday, December 9, 2010

remove comma from a string using split function

Hi frienz,
split function,[ split() ] which is commaonly used here i'm showing an example of that. and collecting o\p in 2 way's using for loop and foreach.
Demo

    public void splitchara( string _content)
    {

        try
           {         
                  char[] delimiterChars = { ','};

            string text = _content; // example:- string conatin 1,2,2,3

            string[] words = text.Split(delimiterChars);

            // using for loop way
           
            string _value = string.Empty;
            for (int i = 0; i < words.Length; i++)
            {
                _value = _value +" "+ words[i].ToString();
            }
            lbl_For.Text = _value.ToString();// showing o/p here

            // using foreach way

            string _lbl=string.Empty;
            foreach (string s in words)
            {
                if (s != "")
                {
                     _lbl =_lbl.ToString() +" "+ s.ToString();
                }           
            }
            lbl_Foreach.Text = _lbl.ToString();// showing o/p here
           }
       catch (Exception ex)
       {
             
              throw;
       }


    }






Have a nice day... 'N happy Coding :)


No comments: