开发者

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

开发者 https://www.devze.com 2023-02-02 12:55 出处:网络
//for e.g. string s=\"this is example\"; //how can i make output 开发者_开发问答like \"This Is Example\"

//for e.g.

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


Try using the below code

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

精彩评论

暂无评论...
验证码 换一张
取 消