Is linq equivalent to javascript join?

In JavaScript, if I have var arr = ["a", "b", "c"] , I can say arr.join(','); to get a string containing a list of values ​​separated by commas. Is there a direct way to do this in Linq?

I know that I can use Aggregate ie pluginNames.Aggregate((s1, s2) => s1 + ", " + s2); But that seems a little awkward. Is there anything cleaner? Something hypothetically similar

 pluginNames.JavaScriptJoin(", "); 
+4
source share
3 answers

Try

 string.Join(", ", pluginNames); 
+15
source

Just use String.Join - not part of LINQ, just framework:

 string joined = string.Join(", ", array); 

If this is really too complicated for you, you can write an extension method:

 public static string JoinStrings(this string[] bits, string separator) { return string.Join(separator, bits); } 

Note that .NET 4 has more overloads for String.Join , including accepting sequences (not just arrays), not just strings.

I would suggest that you are not just using the name Join , as it will look like you are making an internal join.

+14
source

You can use string.Join() :

 string result = string.Join(",", pluginNames); 
+5
source

Source: https://habr.com/ru/post/1385575/


All Articles