Skip to content

Instantly share code, notes, and snippets.

@andrewjk
andrewjk / gist:3186574
Created July 27, 2012 07:00
C# ToTitleCase method
/// <summary>
/// Converts the specified string to title case.
/// </summary>
/// <remarks>
/// This functionality is based off John Gruber's title case function, described at http://daringfireball.net/2008/05/title_case.
///
/// It capitalizes each word in the specified string, with the following exceptions:
/// <list type="bullet">
/// <item><description>Some words are not capitalized, such as "for" and "the".</description></item>
/// <item><description>Words with capitalized letters other than the first, words containing dots and words containing numbers are left unchanged.</description></item>
@andrewjk
andrewjk / gist:3186582
Created July 27, 2012 07:03
C# Pluralize method
/// <summary>
/// Attempts to pluralize the specified text according to the rules of the English language.
/// </summary>
/// <remarks>
/// This function attempts to pluralize as many words as practical by following these rules:
/// <list type="bullet">
/// <item><description>Words that don't follow any rules (e.g. "mouse" becomes "mice") are returned from a dictionary.</description></item>
/// <item><description>Words that end with "y" (but not with a vowel preceding the y) are pluralized by replacing the "y" with "ies".</description></item>
/// <item><description>Words that end with "us", "ss", "x", "ch" or "sh" are pluralized by adding "es" to the end of the text.</description></item>
/// <item><description>Words that end with "f" or "fe" are pluralized by replacing the "f(e)" with "ves".</description></item>
@andrewjk
andrewjk / gist:3186591
Created July 27, 2012 07:08
C# Pluralize tests
[TestMethod]
public void TestEasyNouns()
{
// Easy (i.e. they follow rules) nouns culled from http://www.manythings.org/vocabulary/lists/l/words.php?f=ogden-general_things
Assert.AreEqual("accounts", StringHelper.Pluralize("account"));
Assert.AreEqual("acts", StringHelper.Pluralize("act"));
Assert.AreEqual("additions", StringHelper.Pluralize("addition"));
Assert.AreEqual("adjustments", StringHelper.Pluralize("adjustment"));
Assert.AreEqual("advertisements", StringHelper.Pluralize("advertisement"));
Assert.AreEqual("agreements", StringHelper.Pluralize("agreement"));
@andrewjk
andrewjk / gist:3186594
Created July 27, 2012 07:09
C# ToTitleCase tests
[TestMethod]
public void TestTitleCases()
{
// From https://github.com/ap/titlecase/blob/master/test.pl
Assert.AreEqual("For Step-by-Step Directions Email someone@gmail.com", StringHelper.ToTitleCase("For step-by-step directions email someone@gmail.com"));
Assert.AreEqual("2lmc Spool: 'Gruber on OmniFocus and Vapo(u)rware'", StringHelper.ToTitleCase("2lmc Spool: 'Gruber on OmniFocus and Vapo(u)rware'"));
Assert.AreEqual("Have You Read “The Lottery”?", StringHelper.ToTitleCase("Have you read “The Lottery”?"));
Assert.AreEqual("Your Hair[cut] Looks (Nice)", StringHelper.ToTitleCase("your hair[cut] looks (nice)"));
Assert.AreEqual("People Probably Won't Put http://foo.com/bar/ in Titles", StringHelper.ToTitleCase("People probably won't put http://foo.com/bar/ in titles"));
// NOTE: Replaced the original non-breaking hyphens with regular hyphens