Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions strings-csharp/SplitString/SplitString/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ static void Main(string[] args)
SplitStringUsingCharacterArrayWithoptionsUsingCount(" apple , banana ; cherry,orange ", new[] { ',', ';' }, 3);

Console.WriteLine("\n***************** Split the String Using String Array with String Split Options ***************\n");
SplitStringUsingStringArrayWithoptions("apple,banana,kiwi;grape,mango,orange", new[] { ",", ";" });
SplitStringUsingStringArrayWithoptions("apple,,banana;;kiwi", new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);

Console.WriteLine("\n***************** Split the String Using String Array with String Split Options Using Count ***************\n");
SplitStringUsingStringArrayWithoptionsUsingCount("apple,banana,cherry,orange,pear", new[] { "," }, 3);

Console.WriteLine("\n***************** Split a String Into New Lines ***************\n");
SplitStringIntoNewLines("Line 1\nLine 2\nLine 3\nLine 4\nLine 5", new[] { Environment.NewLine });
SplitStringIntoNewLines("Line 1\nLine 2\nLine 3\nLine 4\nLine 5", ["\r\n", "\n"]);

Console.WriteLine("\n***************** StringSplitOptions on the Same Input ***************\n");
SplitStringWithOptions("a,,b, c, , d ,e", ',', StringSplitOptions.None);
SplitStringWithOptions("a,,b, c, , d ,e", ',', StringSplitOptions.RemoveEmptyEntries);
SplitStringWithOptions("a,,b, c, , d ,e", ',', StringSplitOptions.TrimEntries);
SplitStringWithOptions("a,,b, c, , d ,e", ',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

Console.WriteLine("\n***************** Split a String Without Allocating Substrings ***************\n");
CountSplitRanges("apple,banana,cherry,date", ',');
}

public static string[] SplitStringUsingCharacterArray(string input, char[] separators)
Expand Down Expand Up @@ -73,9 +82,10 @@ public static string[] SplitStringUsingCharacterArrayWithoptionsUsingCount(strin
return result;
}

public static string[] SplitStringUsingStringArrayWithoptions(string input, string[] delimiters)
public static string[] SplitStringUsingStringArrayWithoptions(string input, string[] delimiters,
StringSplitOptions options = StringSplitOptions.None)
{
string[] result = input.Split(delimiters, StringSplitOptions.None);
string[] result = input.Split(delimiters, options);

foreach (string s in result)
{
Expand Down Expand Up @@ -108,5 +118,29 @@ public static string[] SplitStringIntoNewLines(string multiLineText, string[] se

return lines;
}

public static string[] SplitStringWithOptions(string input, char separator, StringSplitOptions options)
{
string[] result = input.Split(separator, options);

Console.WriteLine($"{options}: [{string.Join("] [", result)}]");

return result;
}

public static int CountSplitRanges(string input, char separator)
{
ReadOnlySpan<char> source = input;
Span<Range> ranges = stackalloc Range[8];

int count = source.Split(ranges, separator);

for (int i = 0; i < count; i++)
{
Console.WriteLine(source[ranges[i]].ToString());
}

return count;
}
}
}
2 changes: 1 addition & 1 deletion strings-csharp/SplitString/SplitString/SplitString.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
92 changes: 89 additions & 3 deletions strings-csharp/SplitString/SplitStringTests/SplitStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,100 @@ public void WhenSplittingAStringIntoNewLine_ThenReturnArrayOfSubstrings()
{
// Arrange
string input = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5";
string[] separators = { Environment.NewLine };
int expectedOutputLength = 5;
string[] separators = ["\r\n", "\n"];
string[] expectedOutput = ["Line 1", "Line 2", "Line 3", "Line 4", "Line 5"];

// Act
string[] result = Program.SplitStringIntoNewLines(input, separators);

// Assert
Assert.AreEqual(expectedOutputLength, result.Length);
CollectionAssert.AreEqual(expectedOutput, result);
}

[TestMethod]
public void WhenSplittingCrLfAndLfMixedText_ThenReturnOneEntryPerLineOnEveryPlatform()
{
// Arrange
string input = "Line 1\r\nLine 2\nLine 3\r\nLine 4";
string[] separators = ["\r\n", "\n"];
string[] expectedOutput = ["Line 1", "Line 2", "Line 3", "Line 4"];

// Act
string[] result = Program.SplitStringIntoNewLines(input, separators);

// Assert
CollectionAssert.AreEqual(expectedOutput, result);
}

[TestMethod]
public void WhenSplittingWithNone_ThenKeepEveryPieceAsCut()
{
// Arrange
string input = "a,,b, c, , d ,e";
string[] expectedOutput = ["a", "", "b", " c", " ", " d ", "e"];

// Act
string[] result = Program.SplitStringWithOptions(input, ',', StringSplitOptions.None);

// Assert
CollectionAssert.AreEqual(expectedOutput, result);
}

[TestMethod]
public void WhenSplittingWithRemoveEmptyEntries_ThenDropZeroLengthPiecesOnly()
{
// Arrange
string input = "a,,b, c, , d ,e";
string[] expectedOutput = ["a", "b", " c", " ", " d ", "e"];

// Act
string[] result = Program.SplitStringWithOptions(input, ',', StringSplitOptions.RemoveEmptyEntries);

// Assert
CollectionAssert.AreEqual(expectedOutput, result);
}

[TestMethod]
public void WhenSplittingWithTrimEntries_ThenTrimEveryPieceAndKeepTheEmpties()
{
// Arrange
string input = "a,,b, c, , d ,e";
string[] expectedOutput = ["a", "", "b", "c", "", "d", "e"];

// Act
string[] result = Program.SplitStringWithOptions(input, ',', StringSplitOptions.TrimEntries);

// Assert
CollectionAssert.AreEqual(expectedOutput, result);
}

[TestMethod]
public void WhenSplittingWithRemoveEmptyEntriesAndTrimEntries_ThenTrimFirstThenDrop()
{
// Arrange
string input = "a,,b, c, , d ,e";
string[] expectedOutput = ["a", "b", "c", "d", "e"];

// Act
string[] result = Program.SplitStringWithOptions(input, ',',
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

// Assert
CollectionAssert.AreEqual(expectedOutput, result);
}

[TestMethod]
public void WhenSplittingIntoRanges_ThenReturnThePieceCountWithoutAllocatingSubstrings()
{
// Arrange
string input = "apple,banana,cherry,date";
int expectedCount = 4;

// Act
int count = Program.CountSplitRanges(input, ',');

// Assert
Assert.AreEqual(expectedCount, count);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading