Convert all first letter to upper case, rest lower for each word

前端 未结 11 2027
一向
一向 2020-12-04 10:15

I have a string of text (about 5-6 words mostly) that I need to convert.

Currently the text looks like:

THIS IS MY TEXT RIGHT NOW

I

相关标签:
11条回答
  • 2020-12-04 10:25

    In addition to first answer, remember to change string selectionstart index to the end of the word or you will get reverse order of letters in the string.

    s.SelectionStart=s.Length;
    
    0 讨论(0)
  • 2020-12-04 10:29

    I don't know if the solution below is more or less efficient than jspcal's answer, but I'm pretty sure it requires less object creation than Jamie's and George's.

    string s = "THIS IS MY TEXT RIGHT NOW";
    StringBuilder sb = new StringBuilder(s.Length);
    bool capitalize = true;
    foreach (char c in s) {
        sb.Append(capitalize ? Char.ToUpper(c) : Char.ToLower(c));
        capitalize = !Char.IsLetter(c);
    }
    return sb.ToString();
    
    0 讨论(0)
  • 2020-12-04 10:29

    jspcal's answer as a string extension.

    Program.cs

    class Program
    {
        static void Main(string[] args)
        {
            var myText = "MYTEXT";
            Console.WriteLine(myText.ToTitleCase()); //Mytext
        }
    }
    

    StringExtensions.cs

    using System;
    public static class StringExtensions
    {
    
        public static string ToTitleCase(this string str)
        {
            if (str == null)
                return null;
    
            return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
        }
    }
    
    0 讨论(0)
  • 2020-12-04 10:33

    One of the possible solution you might be interested in. Traversing an array of chars from right to left and vise versa in one loop.

    public static string WordsToCapitalLetter(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("value");
            }
    
            int inputValueCharLength = value.Length;
            var valueAsCharArray = value.ToCharArray();
    
            int min = 0;
            int max = inputValueCharLength - 1;
    
            while (max > min)
            {
                char left = value[min];
                char previousLeft = (min == 0) ? left : value[min - 1];
    
                char right = value[max];
                char nextRight = (max == inputValueCharLength - 1) ? right : value[max - 1];
    
                if (char.IsLetter(left) && !char.IsUpper(left) && char.IsWhiteSpace(previousLeft))
                {
                    valueAsCharArray[min] = char.ToUpper(left);
                }
    
                if (char.IsLetter(right) && !char.IsUpper(right) && char.IsWhiteSpace(nextRight))
                {
                    valueAsCharArray[max] = char.ToUpper(right);
                }
    
                min++;
                max--;
            }
    
            return new string(valueAsCharArray);
        }
    
    0 讨论(0)
  • 2020-12-04 10:36

    Try this technique; It returns the desired result

    CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
    

    And don't forget to use System.Globalization.

    0 讨论(0)
  • 2020-12-04 10:39
    string s = "THIS IS MY TEXT RIGHT NOW";
    
    s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());
    
    0 讨论(0)
提交回复
热议问题