Best way to convert Pascal Case to a sentence

后端 未结 16 1953
天命终不由人
天命终不由人 2020-12-08 13:00

What is the best way to convert from Pascal Case (upper Camel Case) to a sentence.

For example starting with

\"AwaitingFeedback\"

a

16条回答
  •  孤城傲影
    2020-12-08 13:25

    Found myself doing something similar, and I appreciate having a point-of-departure with this discussion. This is my solution, placed as an extension method to the string class in the context of a console application.

    using System;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string piratese = "avastTharMatey";
                string ivyese = "CheerioPipPip";
    
                Console.WriteLine("{0}\n{1}\n", piratese.CamelCaseToString(), ivyese.CamelCaseToString());
                Console.WriteLine("For Pete\'s sake, man, hit ENTER!");
                string strExit = Console.ReadLine();
            }
    
        }
    
        public static class StringExtension
        {
            public static string CamelCaseToString(this string str)
            {
                StringBuilder retVal = new StringBuilder(32);
    
                if (!string.IsNullOrEmpty(str))
                {
                    string strTrimmed = str.Trim();
    
                    if (!string.IsNullOrEmpty(strTrimmed))
                    {
                        retVal.Append(char.ToUpper(strTrimmed[0]));
    
                        if (strTrimmed.Length > 1)
                        {
                            for (int i = 1; i < strTrimmed.Length; i++)
                            {
                                if (char.IsUpper(strTrimmed[i])) retVal.Append(" ");
    
                                retVal.Append(char.ToLower(strTrimmed[i]));
                            }
                        }
                    }
                }
                return retVal.ToString();
            }
        }
    }
    

提交回复
热议问题