Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public static class BackslashMethods
using System.Text.RegularExpressions;

public static class BackslashMethods
{
public static string UsingDoubleBackslash()
{
Expand Down Expand Up @@ -29,4 +31,23 @@ public static string UsingRawStringLiterals()
{
return """C:\Users\User\Documents""";
}

public static bool ContainsBackslash(string input)
{
string pattern = "\\\\";

return Regex.IsMatch(input, pattern);
}

public static bool ContainsBackslashUsingVerbatimPattern(string input)
{
string pattern = @"\\";

return Regex.IsMatch(input, pattern);
}

public static string EscapeForRegex(string text)
{
return Regex.Escape(text);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
Console.WriteLine(BackslashMethods.UsingUnicodeEscapeSequence());
Console.WriteLine(BackslashMethods.UsingStringFormat());
Console.WriteLine(BackslashMethods.UsingStringInterpolation());
Console.WriteLine(BackslashMethods.UsingRawStringLiterals());
Console.WriteLine(BackslashMethods.UsingRawStringLiterals());
Console.WriteLine(BackslashMethods.UsingDoubleBackslash().Length);
Console.WriteLine(BackslashMethods.ContainsBackslash(@"C:\Users\User\Documents"));
Console.WriteLine(BackslashMethods.ContainsBackslashUsingVerbatimPattern(@"C:\Users\User\Documents"));
Console.WriteLine(BackslashMethods.EscapeForRegex(@"C:\Users\User\Documents"));
34 changes: 33 additions & 1 deletion csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,38 @@ public void GivenStringFormatMethod_WhenCallingMethod_ThenReturnFormattedFilePat
[TestMethod]
public void GivenStringInterpolation_WhenCallingMethod_ThenReturnInterpolatedFilePath()
{
Assert.AreEqual(PathUsingDirSeparator, BackslashMethods.UsingStringFormat());
Assert.AreEqual(PathUsingDirSeparator, BackslashMethods.UsingStringInterpolation());
}

[TestMethod]
public void GivenRawStringLiteral_WhenCallingMethod_ThenReturnFilePathWithRawString()
{
Assert.AreEqual(ExpectedPath, BackslashMethods.UsingRawStringLiterals());
}

[TestMethod]
public void GivenDoubleBackslash_WhenCountingCharacters_ThenEachPairIsOneCharacter()
{
Assert.AreEqual(23, BackslashMethods.UsingDoubleBackslash().Length);
}

[TestMethod]
public void GivenRegexPattern_WhenInputHasBackslash_ThenMatch()
{
Assert.IsTrue(BackslashMethods.ContainsBackslash(ExpectedPath));
Assert.IsTrue(BackslashMethods.ContainsBackslashUsingVerbatimPattern(ExpectedPath));
}

[TestMethod]
public void GivenRegexPattern_WhenInputHasNoBackslash_ThenNoMatch()
{
Assert.IsFalse(BackslashMethods.ContainsBackslash("C:/Users/User/Documents"));
Assert.IsFalse(BackslashMethods.ContainsBackslashUsingVerbatimPattern("C:/Users/User/Documents"));
}

[TestMethod]
public void GivenBackslash_WhenEscapingForRegex_ThenBackslashIsDoubled()
{
Assert.AreEqual(@"C:\\Users", BackslashMethods.EscapeForRegex(@"C:\Users"));
}
}
10 changes: 5 additions & 5 deletions csharp-basic-topics/EscapingBackSlashInCSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
<PackageReference Include="coverlet.collector" Version="10.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EscapingBackSlash\EscapingBackSlash.csproj" />
Expand Down
Loading