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
36 changes: 20 additions & 16 deletions strings-csharp/StringToTitleCase/StringToTitleCase/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System;
using System.Globalization;
using System.Globalization;
using StringToTitleCase;

namespace StringToTitleCase
{
class Program
{
static void Main(string[] args)
{
var textinfo = new CultureInfo("en-US", false).TextInfo;
var textinfo = new CultureInfo("en-US", false).TextInfo;

var textInfo = CultureInfo.CurrentCulture.TextInfo;
Console.WriteLine(textinfo.ToTitleCase("a tale oF tWo citIes"));
Console.WriteLine(textinfo.ToTitleCase("harry potter and the Deathly hallows"));

Console.WriteLine(textinfo.ToTitleCase("a tale oF tWo citIes"));
Console.WriteLine(textinfo.ToTitleCase("OLIVER TWIST"));
Console.WriteLine(textinfo.ToTitleCase(textinfo.ToLower("OLIVER TWIST")));
Console.WriteLine(textinfo.ToTitleCase(textinfo.ToLower("THE NASA MISSION")));

Console.WriteLine(textinfo.ToTitleCase("harry potter and the Deathly hallows"));
Console.WriteLine("a tale oF tWo citIes".ToTitleCaseWithMinorWords(textinfo));
Console.WriteLine("harry potter and the Deathly hallows".ToTitleCaseWithMinorWords(textinfo));

Console.WriteLine(textinfo.ToTitleCase("OLIVER TWIST".ToLower()));
}
}
}
Console.OutputEncoding = System.Text.Encoding.UTF8;
var turkish = new CultureInfo("tr-TR", false).TextInfo;
Console.WriteLine(turkish.ToTitleCase("a tale oF tWo citIes"));
Console.WriteLine(turkish.ToTitleCase("istanbul"));
Console.WriteLine(CultureInfo.InvariantCulture.TextInfo.ToTitleCase("a tale oF tWo citIes"));

Console.WriteLine(textinfo.ToTitleCase("the 2nd edition"));
Console.WriteLine(textinfo.ToTitleCase("my iPhone"));
Console.WriteLine(textinfo.ToTitleCase("mcdonald's menu"));
Console.WriteLine(textinfo.ToTitleCase("x-ray vision"));
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Globalization;

namespace StringToTitleCase;

public static class TitleCaseExtensions
{
private static readonly HashSet<string> MinorWords = new(StringComparer.OrdinalIgnoreCase)
{
"a", "an", "and", "as", "at", "but", "by", "for", "in", "nor", "of", "on", "or", "the", "to"
};

public static string ToTitleCaseWithMinorWords(this string input, TextInfo textInfo)
{
var words = textInfo.ToTitleCase(textInfo.ToLower(input)).Split(' ');

for (var i = 1; i < words.Length - 1; i++)
{
if (MinorWords.Contains(words[i]))
{
words[i] = textInfo.ToLower(words[i]);
}
}

return string.Join(' ', words);
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,84 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using StringToTitleCase;

namespace StringToTitleCaseUnitTest
namespace StringToTitleCaseUnitTest;

[TestClass]
public class StringToTitleCaseUnitTest
{
[TestClass]
public class StringToTitleCaseUnitTest
private readonly TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;

[TestMethod]
public void GivenToTitleCase_WhenStringIsPassed_ConversionToTitleCaseSuccessful()
{
private readonly TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
Assert.AreEqual("A Tale Of Two Cities", textInfo.ToTitleCase("a tale oF tWo citIes"));

[TestMethod]
public void GivenToTitleCase_WhenStringIsPassed_CoversionToTitleCaseSuccessful()
{
Assert.AreEqual("A Tale Of Two Cities", textInfo.ToTitleCase("a tale oF tWo citIes"));
Assert.AreEqual("Harry Potter And The Deathly Hallows", textInfo.ToTitleCase("harry potter and the Deathly hallows"));
}

Assert.AreEqual("Harry Potter And The Deathly Hallows", textInfo.ToTitleCase("harry potter and the Deathly hallows"));
}
[TestMethod]
public void GivenToTitleCase_WhenWordWithAllUpperCaseIsPassed_ConversionToTitleCaseNotSuccessful()
{
Assert.AreEqual("OLIVER TWIST", textInfo.ToTitleCase("OLIVER TWIST"));
}

[TestMethod]
public void GivenToTitleCase_WhenWordWithAllUpperCaseIsPassed_ConversionToTitleCaseNotSuccessful()
[TestMethod]
public void GivenToTitleCase_WhenWordWithAllUpperCaseIsPassed_ConversionToTitleCaseSuccessful()
{
Assert.AreEqual("Oliver Twist", textInfo.ToTitleCase(textInfo.ToLower("OLIVER TWIST")));
}

[TestMethod]
public void GivenToTitleCase_WhenAcronymIsLowercasedFirst_AcronymIsLost()
{
Assert.AreEqual("The NASA Mission", textInfo.ToTitleCase("the NASA mission"));

Assert.AreEqual("The Nasa Mission", textInfo.ToTitleCase(textInfo.ToLower("THE NASA MISSION")));
}

[TestMethod]
public void GivenCurrentCultureIsTurkish_WhenStringToLowerIsUsed_DotlessIAppears()
{
var original = CultureInfo.CurrentCulture;
try
{
Assert.AreNotEqual("Oliver Twist", textInfo.ToTitleCase("OLIVER TWIST"));
}
CultureInfo.CurrentCulture = new CultureInfo("tr-TR");

[TestMethod]
public void GivenToTitleCase_WhenWordWithAllUpperCaseIsPassed_ConversionToTitleCaseSuccessful()
Assert.AreEqual("Olıver Twıst", textInfo.ToTitleCase("OLIVER TWIST".ToLower()));
Assert.AreEqual("Oliver Twist", textInfo.ToTitleCase(textInfo.ToLower("OLIVER TWIST")));
}
finally
{
Assert.AreEqual("Oliver Twist", textInfo.ToTitleCase("OLIVER TWIST".ToLower()));
CultureInfo.CurrentCulture = original;
}
}

[TestMethod]
public void GivenToTitleCaseWithMinorWords_WhenStringIsPassed_MinorWordsStayLowercase()
{
Assert.AreEqual("A Tale of Two Cities", "a tale oF tWo citIes".ToTitleCaseWithMinorWords(textInfo));

Assert.AreEqual("Harry Potter and the Deathly Hallows", "harry potter and the Deathly hallows".ToTitleCaseWithMinorWords(textInfo));

Assert.AreEqual("The Lord of the Rings", "the lord of the rings".ToTitleCaseWithMinorWords(textInfo));
}

[TestMethod]
public void GivenToTitleCase_WhenCultureIsTurkish_ResultUsesTurkishCasing()
{
var turkish = new CultureInfo("tr-TR", false).TextInfo;

Assert.AreEqual("A Tale Of Two Citıes", turkish.ToTitleCase("a tale oF tWo citIes"));
Assert.AreEqual("İstanbul", turkish.ToTitleCase("istanbul"));
Assert.AreEqual("A Tale Of Two Cities", CultureInfo.InvariantCulture.TextInfo.ToTitleCase("a tale oF tWo citIes"));
}

[TestMethod]
public void GivenToTitleCase_WhenWordHasDigitsOrInnerCapitals_ResultIsNotWhatAHumanWrites()
{
Assert.AreEqual("The 2Nd Edition", textInfo.ToTitleCase("the 2nd edition"));
Assert.AreEqual("My Iphone", textInfo.ToTitleCase("my iPhone"));
Assert.AreEqual("Mcdonald's Menu", textInfo.ToTitleCase("mcdonald's menu"));
Assert.AreEqual("X-Ray Vision", textInfo.ToTitleCase("x-ray vision"));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<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="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.2" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\StringToTitleCase\StringToTitleCase.csproj" />
</ItemGroup>

</Project>
Loading