diff --git a/PolyShim.Tests/NetCore20/StringTests.cs b/PolyShim.Tests/NetCore20/StringTests.cs index ef40461..b7c5715 100644 --- a/PolyShim.Tests/NetCore20/StringTests.cs +++ b/PolyShim.Tests/NetCore20/StringTests.cs @@ -129,4 +129,46 @@ public void Split_String_Count_Test() str.Split(" ", 2).Should().Equal("a", "b c"); str.Split(" ", 1).Should().Equal("a b c"); } + + [Fact] + public void Join_CharSeparator_ObjectArray_Test() + { + // Act & assert + string.Join(',', new object?[] { 1, "two", 3.0 }).Should().Be("1,two,3"); + string.Join(',', new object?[] { }).Should().Be(""); + string.Join(',', new object?[] { null, "a", null }).Should().Be(",a,"); + } + + [Fact] + public void Join_CharSeparator_StringArray_Test() + { + // Act & assert + string.Join(',', "a", "b", "c").Should().Be("a,b,c"); + string.Join(',', new string?[] { }).Should().Be(""); + string.Join(',', new string?[] { null, "a", null }).Should().Be(",a,"); + } + + [Fact] + public void Join_CharSeparator_EnumerableT_Test() + { + // Act & assert + string.Join(',', new System.Collections.Generic.List { 1, 2, 3 }) + .Should() + .Be("1,2,3"); + string.Join(',', new System.Collections.Generic.List { "a", null, "b" }) + .Should() + .Be("a,,b"); + } + + [Fact] + public void Join_CharSeparator_WithIndexAndCount_Test() + { + // Arrange + var values = new string?[] { "a", "b", "c", "d" }; + + // Act & assert + string.Join(',', values, 1, 2).Should().Be("b,c"); + string.Join(',', values, 0, 4).Should().Be("a,b,c,d"); + string.Join(',', values, 2, 0).Should().Be(""); + } } diff --git a/PolyShim/NetCore10/String.cs b/PolyShim/NetCore10/String.cs index b12972b..a6bfe94 100644 --- a/PolyShim/NetCore10/String.cs +++ b/PolyShim/NetCore10/String.cs @@ -6,6 +6,7 @@ // ReSharper disable PartialTypeWithSinglePart using System; +using System.Collections.Generic; using System.Linq; using System.Diagnostics.CodeAnalysis; @@ -19,6 +20,14 @@ internal static class MemberPolyfills_NetCore10_String // https://learn.microsoft.com/dotnet/api/system.string.isnullorwhitespace public static bool IsNullOrWhiteSpace(string? value) => value is null || value.All(char.IsWhiteSpace); + + // https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-string-system-object()) + public static string Join(string? separator, params object?[] values) => + string.Join(separator, values.Select(v => v?.ToString()).ToArray()); + + // https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-string-system-collections-generic-ienumerable((-0))) + public static string Join(string? separator, IEnumerable values) => + string.Join(separator, values.Select(v => ((object?)v)?.ToString()).ToArray()); } } #endif diff --git a/PolyShim/NetCore20/String.cs b/PolyShim/NetCore20/String.cs index 22e1b78..febe45c 100644 --- a/PolyShim/NetCore20/String.cs +++ b/PolyShim/NetCore20/String.cs @@ -6,7 +6,9 @@ // ReSharper disable PartialTypeWithSinglePart using System; +using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Text; using System.Diagnostics.CodeAnalysis; @@ -140,6 +142,25 @@ public string[] Split( string? separator, StringSplitOptions options = StringSplitOptions.None ) => str.Split([separator ?? ""], options); + + // https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-object()) + public static string Join(char separator, params object?[] values) => + string.Join(separator.ToString(), values.Select(v => v?.ToString()).ToArray()); + + // https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string()) + public static string Join(char separator, params string?[] values) => + string.Join(separator.ToString(), values); + + // https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-char-system-collections-generic-ienumerable((-0))) + public static string Join(char separator, IEnumerable values) => + string.Join( + separator.ToString(), + values.Select(v => ((object?)v)?.ToString()).ToArray() + ); + + // https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string()-system-int32-system-int32) + public static string Join(char separator, string?[] value, int startIndex, int count) => + string.Join(separator.ToString(), value, startIndex, count); } } #endif diff --git a/PolyShim/Signatures.md b/PolyShim/Signatures.md index 0b398ad..11fec0c 100644 --- a/PolyShim/Signatures.md +++ b/PolyShim/Signatures.md @@ -1,8 +1,8 @@ # Signatures -- **Total:** 373 +- **Total:** 379 - **Types:** 79 -- **Members:** 294 +- **Members:** 300 ___ @@ -371,6 +371,12 @@ ___ - [`bool IsNullOrWhiteSpace(string?)`](https://learn.microsoft.com/dotnet/api/system.string.isnullorwhitespace) .NET Core 1.0 - [`bool StartsWith(char)`](https://learn.microsoft.com/dotnet/api/system.string.startswith#system-string-startswith(system-char)) .NET Core 2.0 - [`int GetHashCode(StringComparison)`](https://learn.microsoft.com/dotnet/api/system.string.gethashcode#system-string-gethashcode(system-stringcomparison)) .NET Core 2.0 + - [`string Join(char, params object?[])`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-object())) .NET Core 2.0 + - [`string Join(char, params string?[])`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string())) .NET Core 2.0 + - [`string Join(char, string?[], int, int)`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-char-system-string()-system-int32-system-int32)) .NET Core 2.0 + - [`string Join(string?, params object?[])`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join(system-string-system-object())) .NET Core 1.0 + - [`string Join(char, IEnumerable)`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-char-system-collections-generic-ienumerable((-0)))) .NET Core 2.0 + - [`string Join(string?, IEnumerable)`](https://learn.microsoft.com/dotnet/api/system.string.join#system-string-join-1(system-string-system-collections-generic-ienumerable((-0)))) .NET Core 1.0 - [`string Replace(string, string?, bool, CultureInfo?)`](https://learn.microsoft.com/dotnet/api/system.string.replace#system-string-replace(system-string-system-string-system-boolean-system-globalization-cultureinfo)) .NET Core 2.0 - [`string Replace(string, string?, StringComparison)`](https://learn.microsoft.com/dotnet/api/system.string.replace#system-string-replace(system-string-system-string-system-stringcomparison)) .NET Core 2.0 - [`string ReplaceLineEndings()`](https://learn.microsoft.com/dotnet/api/system.string.replacelineendings#system-string-replacelineendings) .NET 6.0