25
Feb

String.Capitalize in C# (PHP’s ucwords in C#)

I have been playing around with C# recently and found the need for functionality similar to that provided by PHP’s ucwords() function. I dont think this exists in C# as standard so here is a simple extension method to achieve ucwords in C# using a little bit of LINQ (the extremely useful Aggregate extension method) to achieve it:

public static string Capitalize(this String s)
{
  return s.ToCharArray().Aggregate(String.Empty,
    (working, next) =>
      working.Length == 0 && next != ' ' ? next.ToString().ToUpper() : (
        working.EndsWith(" ") ? working + next.ToString().ToUpper() :
          working + next.ToString()
    )
  );
}

With this included in your name space, the extension method can be used on any String instance like:

string myString = "this sentence needs capitalization!";
Console.WriteLine(myString.Capitalize());
//This Sentence Needs Capitalization!

Short but sweet!

  • Pravin
  • http://www.destiny-denied.co.uk/ Daniel Skinner

    That is also a possibility but the functionality of the two methods are not the same (in principle).

    However, the behaviour of that method may change in the future. A more accurate ToTitleCase method would change “war and peace” to “War and Peace” and so the functionality of the ToTitleCase method will improve in future releases.

    The Captialize function I provided is meant to change “war and peace” to “War And Peace”.

  • http://www.TechieNow.com julio

    I suggest adding ToLower() the the last part so it also converts “tHIS sENTENCE” to “This Sentence” rather than “THIS SENTENCE.

    i.e.

    public static string Capitalize(this String s)
    {
    return s.ToCharArray().Aggregate(String.Empty,
    (working, next) =>
    working.Length == 0 && next != ‘ ‘ ? next.ToString().ToUpper() : (
    working.EndsWith(” “) ? working + next.ToString().ToUpper() :
    working + next.ToString().ToLower()
    )
    );
    }

  • http://www.designerhandbags888.com replica handbags sale

    suggest adding ToLower() the the last part so it also converts “tHIS sENTENCE” to “This Sentence” rather than “THIS SENTENCE.

    i.e.

    public static string Capitalize(this String s)
    {
    return s.ToCharArray().Aggregate(String.Empty,
    (working, next) =>
    working.Length == 0 && next != ‘ ‘ ? next.ToString().ToUpper() : (
    working.EndsWith(” “) ? working + next.ToString().ToUpper() :
    working + next.ToString().ToLower()
    )
    );
    }

  • http://www.facebook.com/profile.php?id=100003469737752 Runs

    Super useful set of taelbs and I’m glad that you took the time to put these together.It’s a wonder that Microsoft doesn’t document any of this on MSDN or provide even the useful links that you’ve provided here. Chuck

  • Markus

    Where in my project would I put the source, so it’s in my namespace?
    I just tried to put it into the same class in which I want to use the method, but I get the error “‘string’ does not contain a definition for ‘Capitalize’ and no Capitalize-extension-method was found”

    EDIT: I had to put the method source in a *static* class, this was the solution.

    • Daniel Skinner

      Glad you got it sorted. Extension methods can be added anywhere in your project structure really as long as they are attached to a static class