Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert Pascal Case to a sentence

Tags:

string

c#

People also ask

How do you convert a camel case?

Here, we convert the first string/word in the array to lowercase. For every other word in the array, we convert the first character to uppercase and the rest to lowercase. Let's test this method using white space as the non-word characters: assertThat(toCamelCaseByRegex("THIS STRING SHOULD BE IN CAMEL CASE")) .

What is the difference between CamelCase and Pascal case?

Camel case and Pascal case are similar. Both demand variables made from compound words and have the first letter of each appended word written with an uppercase letter. The difference is that Pascal case requires the first letter to be uppercase as well, while camel case does not.

What is CamelCase example?

Meaning of camel case in English. the use of a capital letter to begin the second word in a compound name or phrase, when it is not separated from the first word by a space: Examples of camel case include "iPod" and "GaGa".

What is Pascal naming convention?

Pascal case -- or PascalCase -- is a programming naming convention where the first letter of each compound word in a variable is capitalized. The use of descriptive variable names is a software development best practice. However, modern programming languages do not allow variables names to include blank spaces.


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


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.


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();
        }
    }
}

This works for me:

Regex.Replace(strIn, "([A-Z]{1,2}|[0-9]+)", " $1").TrimStart()

This is just like @SSTA, but is more efficient than calling TrimStart.

Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")

Found this in the MvcContrib source, doesn't seem to be mentioned here yet.

return Regex.Replace(input, "([A-Z])", " $1", RegexOptions.Compiled).Trim();