Best way to convert Pascal Case to a sentence

后端 未结 16 1922
天命终不由人
天命终不由人 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:27

    Mostly already answered here

    Small chage to the accepted answer, to convert the second and subsequent Capitalised letters to lower case, so change

    if (char.IsUpper(text[i]))                
        newText.Append(' ');            
    newText.Append(text[i]);
    

    to

    if (char.IsUpper(text[i]))                
    {
        newText.Append(' ');            
        newText.Append(char.ToLower(text[i]));
    }
    else
       newText.Append(text[i]);
    
    0 讨论(0)
  • 2020-12-08 13:33

    Here you go...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace CamelCaseToString
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(CamelCaseToString("ThisIsYourMasterCallingYou"));   
            }
    
            private static string CamelCaseToString(string str)
            {
                if (str == null || str.Length == 0)
                    return null;
    
                StringBuilder retVal = new StringBuilder(32);
    
                retVal.Append(char.ToUpper(str[0]));
                for (int i = 1; i < str.Length; i++ )
                {
                    if (char.IsLower(str[i]))
                    {
                        retVal.Append(str[i]);
                    }
                    else
                    {
                        retVal.Append(" ");
                        retVal.Append(char.ToLower(str[i]));
                    }
                }
    
                return retVal.ToString();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 13:36
    public static string ToSentenceCase(this string str)
    {
        return Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLower(m.Value[1]));
    }
    

    In versions of visual studio after 2015, you can do

    public static string ToSentenceCase(this string str)
    {
        return Regex.Replace(str, "[a-z][A-Z]", m => $"{m.Value[0]} {char.ToLower(m.Value[1])}");
    }
    

    Based on: Converting Pascal case to sentences using regular expression

    0 讨论(0)
  • 2020-12-08 13:36

    Most of the preceding answers split acronyms and numbers, adding a space in front of each character. I wanted acronyms and numbers to be kept together so I have a simple state machine that emits a space every time the input transitions from one state to the other.

        /// <summary>
        /// Add a space before any capitalized letter (but not for a run of capitals or numbers)
        /// </summary>
        internal static string FromCamelCaseToSentence(string input)
        {
            if (string.IsNullOrEmpty(input)) return String.Empty;
    
            var sb = new StringBuilder();
            bool upper = true;
    
            for (var i = 0; i < input.Length; i++)
            {
                bool isUpperOrDigit = char.IsUpper(input[i]) || char.IsDigit(input[i]);
                // any time we transition to upper or digits, it's a new word
                if (!upper && isUpperOrDigit)
                {
                    sb.Append(' ');
                }
                sb.Append(input[i]);
                upper = isUpperOrDigit;
            }
    
            return sb.ToString();
        }
    

    And here's some tests:

        [TestCase(null, ExpectedResult = "")]
        [TestCase("", ExpectedResult = "")]
        [TestCase("ABC", ExpectedResult = "ABC")]
        [TestCase("abc", ExpectedResult = "abc")]
        [TestCase("camelCase", ExpectedResult = "camel Case")]
        [TestCase("PascalCase", ExpectedResult = "Pascal Case")]
        [TestCase("Pascal123", ExpectedResult = "Pascal 123")]
        [TestCase("CustomerID", ExpectedResult = "Customer ID")]
        [TestCase("CustomABC123", ExpectedResult = "Custom ABC123")]
        public string CanSplitCamelCase(string input)
        {
            return FromCamelCaseToSentence(input);
        }
    
    0 讨论(0)
提交回复
热议问题