C# 中是否有本机大小写字符串函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2004944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a native Proper Case string function in C#?
提问by Shaul Behr
I was about to write my own C# extension to convert a string to Proper Case (i.e. capitalize the first letter of every word), then I wondered if there's not a native C# function to do just that... is there?
我正要编写自己的 C# 扩展来将字符串转换为正确大小写(即每个单词的第一个字母大写),然后我想知道是否没有本地 C# 函数可以做到这一点......有吗?
采纳答案by Greg Beech
There is a function that capitalises the first letters of words, though you should see the remarks section as it does have some limitations which may make it unsuitable for your needs.
有一个功能可以将单词的第一个字母大写,但您应该看到备注部分,因为它确实有一些限制,可能使其不适合您的需要。
回答by Jamie Dixon
String s = "yOu caN Use thIs"
s = System.Threading.Thread.CurrentThread
.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());
The main limitation I can see with this is that it's not "true" title case. i.e. In the phrase "WaR aNd peaCe", the "and" part should be lowercase in English. This method would capitalise it though.
我可以看到的主要限制是它不是“真实”的标题案例。即在短语“War and peCe”中,“and”部分在英文中应该是小写的。不过,这种方法会将其大写。
回答by TotPeRo
you can just add some exten-sion meth-ods to the Sting type:
你可以在 Sting 类型中添加一些扩展方法:
public static class StringExtension
{
/// <summary>
/// Use the current thread's culture info for conversion
/// </summary>
public static string ToTitleCase(this string str)
{
var cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Overload which uses the culture info with the specified name
/// </summary>
public static string ToTitleCase(this string str, string cultureInfoName)
{
var cultureInfo = new CultureInfo(cultureInfoName);
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Overload which uses the specified culture info
/// </summary>
public static string ToTitleCase(this string str, CultureInfo cultureInfo)
{
return cultureInfo.TextInfo.ToTitleCase(str.ToLower());
}
}
回答by user2987826
This should work.
这应该有效。
public static string ToTitleCase(this string strX)
{
string[] aryWords = strX.Trim().Split(' ');
List<string> lstLetters = new List<string>();
List<string> lstWords = new List<string>();
foreach (string strWord in aryWords)
{
int iLCount = 0;
foreach (char chrLetter in strWord.Trim())
{
if (iLCount == 0)
{
lstLetters.Add(chrLetter.ToString().ToUpper());
}
else
{
lstLetters.Add(chrLetter.ToString().ToLower());
}
iLCount++;
}
lstWords.Add(string.Join("", lstLetters));
lstLetters.Clear();
}
string strNewString = string.Join(" ", lstWords);
return strNewString;
}
回答by Rick
This works
这有效
public static string ConvertTo_ProperCase(string text)
{
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
return myTI.ToTitleCase(text.ToLower());
}