6

//for e.g.

string s="this is example";

//how can i make output like "This Is Example"

using too simple code in c#??

Neo
  • 15,491
  • 59
  • 215
  • 405
  • Duplicate: http://stackoverflow.com/questions/1943273/convert-all-first-letter-to-upper-case-rest-lower-for-each-word – CD.. Jan 06 '11 at 07:11

3 Answers3

10

Try this.

String s = "this is example";
Console.WriteLine(Thread.CurrentCulture.TextInfo.ToTitleCase(s));
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • I would not use `ToLower`. `ToTitleCase` is sufficient. – leppie Jan 06 '11 at 07:14
  • +1, but with a note that in English (and other languages), making the first letter of every word uppercase is not "linguistically correct" as the [MSDN article](http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx) points out. So this method, even though it's called `ToTitleCase` doesn't actually capitalize the string according to the [title case rules](http://en.wikipedia.org/wiki/Letter_case#Choice_of_case_in_text). – Allon Guralnek Jan 06 '11 at 07:17
  • 1
    I think it should be `Thread.CurrentThread.CurrentCulture...` – toni Nov 21 '12 at 11:13
7

What you're describing is sometimes called ProperCase, or in C# case, TitleCase. It might seem like overkill, but as far as I know it takes some 'cultural' localization information. Luckily you can just default to the one currently in use.

CultureInfo c   = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = c.TextInfo;

String newString = textInfo.ToTitleCase(oldString);

Of course in practice you'll probably want to put it all together like Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase, but it can't hurt to see what all that crap means.

http://support.microsoft.com/kb/312890

jon_darkstar
  • 16,398
  • 7
  • 29
  • 37
0

Try using the below code

Console.WriteLine(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str));
misguided
  • 3,699
  • 21
  • 54
  • 96
Rajesh
  • 1