Is it possible to Convert a string to Title Case in Xamarin.Forms?

前端 未结 5 882
遇见更好的自我
遇见更好的自我 2021-01-22 13:46

So in my program I have an Entry like so:


and this is bound to a property.

I ha

5条回答
  •  情深已故
    2021-01-22 14:00

    I find a good solution on the following link:

    https://www.codeproject.com/Tips/1004964/Title-Case-in-VB-net-or-Csharp

    this solution take care about first-letter-capital and escape words such as the, a, in

    public static class StringExtensions
        {
            public static string ToTitleCase(this string s)
            {
    
            var upperCase = s.ToUpper();
            var words = upperCase.Split(' ');
    
            var minorWords = new String[] {"ON", "IN", "AT", "OFF", "WITH", "TO", "AS", "BY",//prepositions
                                       "THE", "A", "OTHER", "ANOTHER",//articles
                                       "AND", "BUT", "ALSO", "ELSE", "FOR", "IF"};//conjunctions
    
            var acronyms = new String[] {"UK", "USA", "US",//countries
                                       "BBC",//TV stations
                                       "TV"};//others
    
            //The first word.
            //The first letter of the first word is always capital.
            if (acronyms.Contains(words[0]))
            {
                words[0] = words[0].ToUpper();
            }
            else
            {
                words[0] = words[0].ToPascalCase();
            }
    
            //The rest words.
            for (int i = 0; i < words.Length; i++)
            {
                if (minorWords.Contains(words[i]))
                {
                    words[i] = words[i].ToLower();
                }
                else if (acronyms.Contains(words[i]))
                {
                    words[i] = words[i].ToUpper();
                }
                else
                {
                    words[i] = words[i].ToPascalCase();
                }
            }
    
            return string.Join(" ", words);
    
        }
    
        public static string ToPascalCase(this string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                return s.Substring(0, 1).ToUpper() + s.Substring(1).ToLower();
            }
            else
            {
                return String.Empty;
            }
        }
    }
    

提交回复
热议问题