Friday, 20 March 2015

convert list to string c#

There are many ways you will do convert list to string c# from google. But what you will adopt, which is good in terms of line and in terms of complexity.

 I will demonstrate three ways to convert list to string c# with “,” seperated  and you will see that which one is taking less time in execution.
Suppose you have list of sting, that you can form like below code; Having 1Lakhs of string records



Code snippet
List<string> strC = new List<string>();
            for (int i = 0; i <= 10000; i++)
                strC.Add("string " + i.ToString());

Convert list to string - 1st way
Just assign the array values to one of the string and remove the last character like below code;
foreach (string o in strC)
            {

                str += o;
            }
str.Remove(strC.Count - 1);

Convert list to string - 2nd way
Just assign the first value to the string than check for the second values and assign it to the string with the “,” separated values.
Console.WriteLine(" 2) Start : - " + DateTime.Now);
            foreach (string o in strC)
            {
                if (first)
                {
                    str2 += o;
                    first = false;
                }
                else
                {
                    str2 += "," + o;
                }
            }

Convert list to string - 3rd Way
By doing simple API of string.join. It will concatenate the string array into the string values.
Console.WriteLine(" 3) Start : - " + DateTime.Now);
            string str3 = string.Join(",", strC.ToArray());

You can see the full code of converting the string array into the string type in C# with  “,” separated values.
Code snippet
  static void Main(string[] args)
        {
            List<string> strC = new List<string>();
            for (int i = 0; i <= 100000; i++)
                strC.Add("string " + i.ToString());

            string str = string.Empty;
            bool first = true;
         

            Console.WriteLine(" 1) Start : - " + DateTime.Now);
            foreach (string o in strC)
            {

                str += o;
            }
              str.Remove(strC.Count - 1);
            //  Console.WriteLine(str);
            Console.WriteLine("End : - " + DateTime.Now);

            string str2 = string.Empty;
            Console.WriteLine(" 2) Start : - " + DateTime.Now);
            foreach (string o in strC)
            {
                if (first)
                {
                    str2 += o;
                    first = false;
                }
                else
                {
                    str2 += "," + o;
                }
            }
            // Console.WriteLine(str);
            Console.WriteLine("End : - " + DateTime.Now);

            Console.WriteLine(" 3) Start : - " + DateTime.Now);
            string str3 = string.Join(",", strC.ToArray());
            Console.WriteLine("End : - " + DateTime.Now);
        }

Now run the application you can see which will take much time.


No comments:

Post a Comment