Converting A String To Different Cases And Humanizer

We can convert a string to different cases using C# code or Visual Studio shortcuts like Ctrl + U & Ctrl + Shift + U. Along with it, we can also use other combination box selection (vertical selection or rectangular selection) Ctrl + Shift + Arrow (Left Arrow, Right Arrow, Up Arrow, Down Arrow) or you can also use Ctrl + Shift + Mouse pointer to select a rectangular area. In the below image, you can see how box selection can be used with change case shortcuts.

shortcuts

This is not limited and if you do not want to just convert case of string but convert a string to a sentence you can use Humanizer for which if we combine all these, it will look something similar t the image shown below.

shortcuts

Let’s start one by one to understand it in a better way.

Convert a string to Lower case

  1. Convert a string to Lower case : Using shortcut

    "WelcomeToCaseConversionLearning" => "welcometocaseconversionlearning"

    shortcuts

  2. Convert a string to Lower case: Using String class methods.
    1. string txt = "WelcomeToCaseConversionLearning";  
    2. Console.WriteLine(txt.ToLower());  
    Output

    welcometocaseconversionlearning

  3. Convert a string to Lower case: Using TextInfo class methods.
    1. string txt = "WelcomeToCaseConversionLearning";  
    2. TextInfo textinfo = new CultureInfo("en-US"false).TextInfo;   
    3. WriteLine(textinfo.ToLower(txt));  
    Output

    welcometocaseconversionlearning

  4. Convert a string to Lower case sentence : Using Humanizer

    If you would like that "WelcomeToCaseConversionLearning" should be converted to “welcome to case conversion learning”, you can use Humanizer.

    i.e. "WelcomeToCaseConversionLearning" => “welcome to case conversion learning” use code shown below,
    1. string txt = "WelcomeToCaseConversionLearning";  
    2. Console.WriteLine(txt.Humanize(LetterCasing.LowerCase));  
    Output

    Welcome to case conversion learning

    Humanizer is a portable class library that can be used for manipulation of strings, enums, numbers, quantities, dates, times, timespans etc.

Convert a string to Upper case

  1. Convert a string to Upper case: Using shortcut

    "welcometocaseconversionlearning" => "WELCOMETOCASECONVERSIONLEARNING"

    shortcuts

  2. Convert a string to Upper case: Using String class methods,
    1. string txt = "welcometocaseconversionlearning";  
    2. Console.WriteLine(txt.ToUpper());  
    Output

    WELCOMETOCASECONVERSIONLEARNING

  3. Convert a string to Upper case: Using TextInfo class methods,
    1. string txt = "welcometocaseconversionlearning";  
    2. TextInfo textinfo = new CultureInfo("en-US"false).TextInfo;  
    3. WriteLine(textinfo.ToUpper(txt));  
    Output

    WELCOMETOCASECONVERSIONLEARNING

  4. Using Humanizer, convert a sentence with all the characters in Upper case

    i.e. "welcomeToCaseConversionLearning " => “WELCOME TO CASE CONVERSION LEARNING” use below code,
    1. string txt = "welcomeToCaseConversionLearning";  
    2. Console.WriteLine(txt.Humanize(LetterCasing.AllCaps)); 
    Output

    WELCOME TO CASE CONVERSION LEARNING

Converting Pascal Case to Camel Case

  1. Converting Pascal Case to Camel Case: Using shortcut

    You can just select the first character of the string and use the shortcut “Ctrl+U” and it will convert the first character of the string to Lower case.

    The shortcut option is not limited to C#. Shortcut option can be used with any language opened inside Visual Studio because it is a feature of Visual Studio.

    Let’s do Some More Conversion


    Convert
    1. <p ng-bind="EmployeeDetails.FirstName"></p>  
    2. <p ng-bind="EmployeeDetails.MiddleName"></p>  
    3. <p ng-bind="EmployeeDetails.LastName"></p>  
    4. <p ng-bind="EmployeeDetails.CurrentDesignation"></p>  
    5. <p ng-bind="EmployeeDetails.Salary"></p>  
    6. <p ng-bind="EmployeeDetails.Age"></p>  
    7. <p ng-bind="EmployeeDetails.MobileNumber"></p>  
    8. <p ng-bind="EmployeeDetails.EmailId"></p>  
    9. <p ng-bind="EmployeeDetails.FacebookId"></p>  
    10. <p ng-bind="EmployeeDetails.TwitterId"></p>  
    To
    1. <p ng-bind="employeeDetails.firstName"></p>  
    2. <p ng-bind="employeeDetails.middleName"></p>  
    3. <p ng-bind="employeeDetails.lastName"></p>  
    4. <p ng-bind="employeeDetails.currentDesignation"></p>  
    5. <p ng-bind="employeeDetails.salary"></p>   
    6. <p ng-bind="employeeDetails.age"></p>  
    7. <p ng-bind="employeeDetails.mobileNumber"></p>  
    8. <p ng-bind="employeeDetails.emailId"></p>  
    9. <p ng-bind="employeeDetails.facebookId"></p>  
    10. <p ng-bind="employeeDetails.twitterId"></p>  
    As we know, most of the popular text editors like Visual Studio, Notepad++, Sublime etc. provides the feature of box selection (Vertical Selection). Hence, select the first character of the string “EmployeeDetails” vertically and the press Ctrl+U. Again, select first character of each attribute vertically and then press Ctrl+U.

    e.g.

    code

    Press Ctrl+U and repeat same for the attributes of employee details.

    For box selection we use the shortcut key Ctrl+Shift+ Arrow

    1. Ctrl + Shift + Down Arrow => select vertically down.
    2. Ctrl + Shift + Up Arrow => select vertically up
    3. Ctrl + Shift + Right Arrow => select horizontally right
    4. Ctrl + Shift + Left Arrow => select horizontally left

    Or you can use Ctrl + Shift and drag mouse to select a rectangular area.

    We can also change multiple line vertically e.g. change EmployeeDetails to empDtls

    code
    Vertical selection provides a lot more features but I am not going to explain all those features here.

  2. Converting Pascal Case to Camel Case: Using String class methods
    1. string txt = "WelcomeToCaseConversionLearning";  
    2. var txt2 = txt.Insert(0, txt[0].ToString().ToLower()).Remove(1, 1);  
    In the same way, you can use methods for conversion to other case types.

    Have a look at the code snippet shown below which will provide you with  the complete idea about the method available for case conversion in TextInfo & String class.
    1. #region String Class Methods  
    2. //Case Conversion Example using String Class Methods  
    3. string txt = "WelcomeToCaseConversionLearning";  
    4.   
    5. //Converting a string to upper case.  
    6. WriteLine(txt.ToUpper());  
    7.   
    8. //Converting a string to lower case.  
    9. WriteLine(txt.ToLower());  
    10. #endregion  
    11.  
    12. #region TextInfo Class Methods  
    13. //Case Conversion Example using TextInfo Class Methods   
    14. string txt2 = "welcomeTocaseconversionLEarning";  
    15. TextInfo textinfo = new CultureInfo("en-US"false).TextInfo;  
    16.   
    17. //Converting a string to upper case.  
    18. WriteLine(textinfo.ToUpper(txt2));  
    19.   
    20. //Converting a string to lower case.  
    21. WriteLine(textinfo.ToLower(txt2));  
    22.   
    23. //Converting a string to Title case  
    24. WriteLine(textinfo.ToTitleCase("welcome To CASe conversion LEArning"));  
    25. #endregion  

Until now, you may have been thinking that if we can achieve something easily using String class or TextInfo class methods then why I am using Humanizer? Let me show you some examples after which you will understand why we need Humanizer.

  1. Convert a string to pascal case
    1. WriteLine("welcomeTo_CaseConversion_Learning".Pascalize());  
    2. //output: WelcomeToCaseConversionLearning  
  2. Convert a string to camel case
    1. WriteLine("WelcomeTo_Case_Conversion_Learning".Camelize());  
    2. //output :welcomeToCaseConversionLearning  
  3. Convert a string to lower case.
    1. WriteLine(txt3.Humanize(LetterCasing.LowerCase));  
    2. //output: welcome to case conversion learning  
  4. Convert a string to upper case.
    1. WriteLine(txt3.Humanize(LetterCasing.AllCaps));  
    2. //Output: WELCOME TO CASE CONVERSION LEARNING  
  5. Convert a string to sentence case.
    1. WriteLine(txt3.Humanize(LetterCasing.Sentence));  
    2. //output: Welcome to case conversion learning  
  6. Convert a string to Title Case
    1. WriteLine(txt3.Humanize(LetterCasing.Title));  
    2. //Output: Welcome To Case Conversion Learning  
  7. Pluralize a string
    1. WriteLine("Employee".Pluralize());  
    2. //Output: Employees  
  8. Singularize a string
    1. WriteLine("Employees".Singularize());  
    2. //Output: Employee  
  9. Add Underscore
    1. WriteLine("Welcome to case conversion learning".Underscore());  
    2. //Output: welcome_to_case_conversion_learning  
  10. Convert underscore to dash or hyphen
    1. WriteLine("welcome_to_case_conversion_learning".Hyphenate());  
    2. WriteLine("welcome_to_case_conversion_learning".Dasherize());  
    3. //Output : welcome-to-case-conversion-learning  
    4. //Output : welcome-to-case-conversion-learning  

Apart from these examples, you can do hundreds of other types of conversion and formatting using Humanizer. For more details, visit here