简体   繁体   中英

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

//for eg

string s="this is example";

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

using too simple code in c#??

Try this.

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

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

尝试使用以下代码

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM