Best way to convert Pascal Case to a sentence

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

    This works for me:

    Regex.Replace(strIn, "([A-Z]{1,2}|[0-9]+)", " $1").TrimStart()
    
    0 讨论(0)
  • 2020-12-08 13:12

    I'd use a regex, inserting a space before each upper case character, then lowering all the string.

        string spacedString = System.Text.RegularExpressions.Regex.Replace(yourString, "\B([A-Z])", " \k");
        spacedString = spacedString.ToLower();
    
    0 讨论(0)
  • 2020-12-08 13:15

    Just because everyone has been using Regex (except this guy), here's an implementation with StringBuilder that was about 5x faster in my tests. Includes checking for numbers too.

    "SomeBunchOfCamelCase2".FromCamelCaseToSentence == "Some Bunch Of Camel Case 2"
    
    public static string FromCamelCaseToSentence(this string input) {
        if(string.IsNullOrEmpty(input)) return input;
    
        var sb = new StringBuilder();
        // start with the first character -- consistent camelcase and pascal case
        sb.Append(char.ToUpper(input[0]));
    
        // march through the rest of it
        for(var i = 1; i < input.Length; i++) {
            // any time we hit an uppercase OR number, it's a new word
            if(char.IsUpper(input[i]) || char.IsDigit(input[i])) sb.Append(' ');
            // add regularly
            sb.Append(input[i]);
        }
    
        return sb.ToString();
    }
    
    0 讨论(0)
  • 2020-12-08 13:17

    An xquery solution that works for both UpperCamel and lowerCamel case:

    To output sentence case (only the first character of the first word is capitalized):

    declare function content:sentenceCase($string)
    {
    let $firstCharacter := substring($string, 1, 1)
    let $remainingCharacters := substring-after($string, $firstCharacter)
    return
    concat(upper-case($firstCharacter),lower-case(replace($remainingCharacters, '([A-Z])', ' $1')))
    };
    

    To output title case (first character of each word capitalized):

    declare function content:titleCase($string)
    {
    let $firstCharacter := substring($string, 1, 1)
    let $remainingCharacters := substring-after($string, $firstCharacter)
    return
    concat(upper-case($firstCharacter),replace($remainingCharacters, '([A-Z])', ' $1'))
    };
    
    0 讨论(0)
  • 2020-12-08 13:19

    I will prefer to use Humanizer for this. Humanizer is a Portable Class Library that meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities.

    Short Answer

    "AwaitingFeedback".Humanize() => Awaiting feedback
    

    Long and Descriptive Answer

    Humanizer can do a lot more work other examples are:

    "PascalCaseInputStringIsTurnedIntoSentence".Humanize() => "Pascal case input string is turned into sentence"
    "Underscored_input_string_is_turned_into_sentence".Humanize() => "Underscored input string is turned into sentence"
    "Can_return_title_Case".Humanize(LetterCasing.Title) => "Can Return Title Case"
    "CanReturnLowerCase".Humanize(LetterCasing.LowerCase) => "can return lower case"
    

    Complete code is :

    using Humanizer;
    using static System.Console;
    
    namespace HumanizerConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                WriteLine("AwaitingFeedback".Humanize());
                WriteLine("PascalCaseInputStringIsTurnedIntoSentence".Humanize());
                WriteLine("Underscored_input_string_is_turned_into_sentence".Humanize());
                WriteLine("Can_return_title_Case".Humanize(LetterCasing.Title));
                WriteLine("CanReturnLowerCase".Humanize(LetterCasing.LowerCase));
            }
        }
    }
    

    Output

    Awaiting feedback

    Pascal case input string is turned into sentence

    Underscored input string is turned into sentence Can Return Title Case

    can return lower case

    If you prefer to write your own C# code you can achieve this by writing some C# code stuff as answered by others already.

    0 讨论(0)
  • 2020-12-08 13:19
    string camel = "MyCamelCaseString";
    string s = Regex.Replace(camel, "([A-Z])", " $1").ToLower().Trim();
    Console.WriteLine(s.Substring(0,1).ToUpper() + s.Substring(1));
    

    Edit: didn't notice your casing requirements, modifed accordingly. You could use a matchevaluator to do the casing, but I think a substring is easier. You could also wrap it in a 2nd regex replace where you change the first character

    "^\w"
    

    to upper

    \U (i think)
    
    0 讨论(0)
提交回复
热议问题