简体   繁体   English

如何使用c#代码制作每个单词的第一个字母

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

//for eg //例如

string s="this is example"; string s =“这是例子”;

//how can i make output like "This Is Example" //如何输出“This Is Example”

using too simple code in c#?? 在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. 您所描述的有时称为ProperCase,或者在C#案例中称为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. 当然在实践中你可能想把它们放在一起像Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase一样,但是看看所有那些垃圾意味着什么都不会有害。

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

尝试使用以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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