string - working - totitlecase r




Format string to title case (4)

How do I format a string to title case?


Here is a simple static method to do this in C#:

public static string ToTitleCaseInvariant(string targetString)
{
    return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(targetString);
}


No, in most languages you can't use regular expressions to do that. An exception to this is Perl which has a particularly powerful "regular" expression syntax.

You will probably find that your language has a library function that can do it. Look for something like s.titlecase().

Related:


Can I apply word-casing to a string using regular expressions?

In Perl:

$string =~ s/([\w']+)/\u\L$1/g;

(taken from the Perl FAQ)





title-case