Neo
Neo

Reputation: 16219

how to make 1st letter of each word capital using c# code

//for e.g.

string s="this is example";

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

using too simple code in c#??

Upvotes: 6

Views: 4440

Answers (3)

Rajesh
Rajesh

Reputation: 1

Try using the below code

Console.WriteLine(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str));

Upvotes: 0

jon_darkstar
jon_darkstar

Reputation: 16768

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

Upvotes: 7

Chandu
Chandu

Reputation: 82913

Try this.

String s = "this is example";
Console.WriteLine(Thread.CurrentCulture.TextInfo.ToTitleCase(s));

Upvotes: 10

Related Questions