2

I want to format my string into Capitalize character formatting (i.e Capitalize First Char Of Words).

For Example - 
              If Input is: "NEW YORK CITY"
              then the desired output is: "New York City"

*my string have maximum 3 words.

After, googled it i found several of ways to achieve this approach, but i can't get which is the best approach for this.

Method 1st:

string City = "NEW YORK CITY";
City = City.ToLower();
string Capatilize_City = "";
Capatilize_City = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(City);

Demo : Ideone with 1st method

Method 2nd:

string City = "NEW YORK CITY";
string[] lstWord = City.ToLower().Split(' ');
string Capatilize_City = "";
foreach (string s in lstWord)
{
  string z = s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);
  Capatilize_City += " " + z;
}
Capatilize_City = Capatilize_City.Trim();

Demo: Ideone with 2nd method

Which code is best for use (performance and speed vice)?

Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
  • your second method has lots of allocations, so my guess is that it would be slower. A profiler can give you the data to tell. – Steve Mitcham Jan 16 '15 at 06:41
  • Try to avoid manual. Use method-1. It is also readable and shorter. – Amit Jan 16 '15 at 06:41
  • 1
    The only way to know which is faster is to test them. Chances are, both are fast enough, in which case go with the simpler. – dbc Jan 16 '15 at 06:42
  • 1
    Unless you are trying to title case a copy of "War & Peace" then performance shouldn't be an issue with either method. – Enigmativity Jan 16 '15 at 06:45

2 Answers2

5

You should go for code size, readability, understandability, maintainability, so the clear winner is...


EDIT

So I'd propose

Capatilize_City =
  System.Globalization.CultureInfo.CurrentCulture.TextInfo
      .ToTitleCase(City.ToLower());
DrKoch
  • 9,556
  • 2
  • 34
  • 43
1

This one liner also works:

string Capatilize_City =
    String.Join(
        " ",
        City
            .ToLower()
            .Split(' ')
            .Select(s =>
                s.Substring(0, 1).ToUpper()
                + s.Substring(1, s.Length - 1)));

Unless you are doing this over a massive string performance shouldn't be an issue for any method.

I'd suggest that you make yourself an extension method on strings called .ToTitleCase() and use that - then if you need to change the implementation you can do it in just one spot in your code.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Why would someone use this instead of the existing ToTitleCase()? – webnoob Jan 16 '15 at 15:32
  • I wouldn't use it, except for the fact that the OP put forward a semantically equivalent approach. I was just providing a "cleaner" way to approach his method. Also there may be special cases (pardon the pun) - like the acronym issue - that the OP wants to cover and the existing `ToTitleCase()` may not do the right thing for the OP. Why else would he raise this question? – Enigmativity Jan 16 '15 at 23:08