Best way to convert Pascal Case to a sentence

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

    Here's a basic way of doing it that I came up with using Regex

    public static string CamelCaseToSentence(this string value)
    {
        var sb = new StringBuilder();
        var firstWord = true;
    
        foreach (var match in Regex.Matches(value, "([A-Z][a-z]+)|[0-9]+"))
        {
            if (firstWord)
            {
                sb.Append(match.ToString());
                firstWord = false;
            }
            else
            {
                sb.Append(" ");
                sb.Append(match.ToString().ToLower());
            }
        }
    
        return sb.ToString();
    }
    

    It will also split off numbers which I didn't specify but would be useful.

提交回复
热议问题