Appending a string in a loop in effective way

前端 未结 5 2204
孤城傲影
孤城傲影 2020-12-05 21:15

for long time , I always append a string in the following way.

for example if i want to get all the employee names separated by some symbol , in the below example i

5条回答
  •  囚心锁ツ
    2020-12-05 21:53

    You should join your strings.

    Example (borrowed from MSDN):

    using System;
    
    class Sample {
        public static void Main() {
        String[] val = {"apple", "orange", "grape", "pear"};
        String sep   = ", ";
        String result;
    
        Console.WriteLine("sep = '{0}'", sep);
        Console.WriteLine("val[] = {{'{0}' '{1}' '{2}' '{3}'}}", val[0], val[1], val[2], val[3]);
        result = String.Join(sep, val, 1, 2);
        Console.WriteLine("String.Join(sep, val, 1, 2) = '{0}'", result);
        }
    }
    

提交回复
热议问题