开发者

Is there a native Proper Case string function in C#? [duplicate]

开发者 https://www.devze.com 2022-12-15 06:31 出处:网络
This question already has answers here: Converting string to title case (23 answers) Closed 5 years ago.
This question already has answers here: Converting string to title case (23 answers) Closed 5 years ago.

I was about to write my own C# extension to convert a string to Proper Case (i.e. capitalize the first letter of every wor开发者_StackOverflow社区d), then I wondered if there's not a native C# function to do just that... is there?


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.


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.


you can just add some exten­sion meth­ods to the String type:

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());
    }
}


This works

public static string ConvertTo_ProperCase(string text) 
{
    TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
    return myTI.ToTitleCase(text.ToLower());
}      


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;
}
0

精彩评论

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