StringJoin 1.0.0
dotnet add package StringJoin --version 1.0.0
NuGet\Install-Package StringJoin -Version 1.0.0
<PackageReference Include="StringJoin" Version="1.0.0" />
paket add StringJoin --version 1.0.0
#r "nuget: StringJoin, 1.0.0"
// Install StringJoin as a Cake Addin #addin nuget:?package=StringJoin&version=1.0.0 // Install StringJoin as a Cake Tool #tool nuget:?package=StringJoin&version=1.0.0
StringJoin
This package provides an extension method called .Join() that joins multiple strings into one string.
Why?
At this point you must be saying "Wait a minute, but we already have string.Join()
,
why do we need another method to do the same thing?"
The reasons are several.
Reason #1: Type safety
Consider this:
var sales = new[] {
new {Amount = 100, Customer = new {Id = 1, Name = "Bob"}},
new {Amount = 200, Customer = new {Id = 2, Name = "Jane"}},
new {Amount = 150, Customer = new {Id = 3, Name = "Alice"}},
};
string top3Customers = string.Join(", ", sales
.GroupBy(s => s.Customer.Id)
.OrderByDescending(g => g.Sum(sale => sale.Amount))
.Take(3));
Console.WriteLine(top3Customers);
What do you think it'll print? For me it printed:
System.Linq.Grouping`2[System.Int32,<>f__AnonymousType0`2[System.Int32,<>f__AnonymousType1`2[System.Int32,System.String
]]], System.Linq.Grouping`2[System.Int32,<>f__AnonymousType0`2[System.Int32,<>f__AnonymousType1`2[System.Int32,System.
String]]], System.Linq.Grouping`2[System.Int32,<>f__AnonymousType0`2[System.Int32,<>f__AnonymousType1`2[System.Int32,
System.String]]]
Not what you expected? Been there.
Things is, string.Join
takes an IEnumerable<object>
and calls .ToString()
on each object.
Such errors could be easily avoided if the method took an IEnumerable<string>
. Then you would catch the error at compile time.
Reason #2: Readability
Let's fix the above code:
string top3Customers = string.Join(", ", sales
.GroupBy(s => s.Customer.Id)
.OrderByDescending(g => g.Sum(sale => sale.Amount))
.Take(3)
.Select(gr => gr.First().Customer.Name));
Now, let's rewrite it using the .Join() method from this library:
string top3Customers = sales
.GroupBy(s => s.Customer.Id)
.OrderByDescending(g => g.Sum(sale => sale.Amount))
.Take(3)
.Select(gr => gr.First().Customer.Name)
.Join(", ");
To me this reads better because:
- There's less parens nesting (()).
- The Join() is chained like Select() and Take(), etc, giving the statement a slick, easy to parse look.
- All the formatting code is in one place: we Select customer Names and Join them using commas.
Reason #3: Char delimiter
There's an overload with a char
delimiter which I think is very nice to have.
Summing up
Reason #1 above got me so many times that I finally decided to write my own extension method that would save me from those errors once and for all. In fact, I've been using this extension method for quite a while and got so used to it that I began to miss it in other projects. That's when I knew I had to make it into a Nuget package.
See also
If you generate lots of strings, it may be not the best idea to join them into one huge string in memory.
You may want to convert your IEnumerable<string> to a Stream.
That's exactly what one of my other packages does: https://github.com/bzaar/EnumerableToStream.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.0
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 135 | 8/28/2024 |