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.
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.
Let’s start one by one to understand it in a better way.
Convert a string to Lower case
- Convert a string to Lower case : Using shortcut
"WelcomeToCaseConversionLearning" => "welcometocaseconversionlearning"
- Convert a string to Lower case: Using String class methods.
- string txt = "WelcomeToCaseConversionLearning";
- Console.WriteLine(txt.ToLower());
Output
welcometocaseconversionlearning
- Convert a string to Lower case: Using TextInfo class methods.
- string txt = "WelcomeToCaseConversionLearning";
- TextInfo textinfo = new CultureInfo("en-US", false).TextInfo;
- WriteLine(textinfo.ToLower(txt));
Output
welcometocaseconversionlearning
- 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,
- string txt = "WelcomeToCaseConversionLearning";
- 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
- Convert a string to Upper case: Using shortcut
"welcometocaseconversionlearning" => "WELCOMETOCASECONVERSIONLEARNING"
- Convert a string to Upper case: Using String class methods,
- string txt = "welcometocaseconversionlearning";
- Console.WriteLine(txt.ToUpper());
Output
WELCOMETOCASECONVERSIONLEARNING
- Convert a string to Upper case: Using TextInfo class methods,
- string txt = "welcometocaseconversionlearning";
- TextInfo textinfo = new CultureInfo("en-US", false).TextInfo;
- WriteLine(textinfo.ToUpper(txt));
Output
WELCOMETOCASECONVERSIONLEARNING
- Using Humanizer, convert a sentence with all the characters in Upper case
i.e. "welcomeToCaseConversionLearning " => “WELCOME TO CASE CONVERSION LEARNING” use below code,
- string txt = "welcomeToCaseConversionLearning";
- Console.WriteLine(txt.Humanize(LetterCasing.AllCaps));
Output
WELCOME TO CASE CONVERSION LEARNING
Converting Pascal Case to Camel Case
- 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
- <p ng-bind="EmployeeDetails.FirstName"></p>
- <p ng-bind="EmployeeDetails.MiddleName"></p>
- <p ng-bind="EmployeeDetails.LastName"></p>
- <p ng-bind="EmployeeDetails.CurrentDesignation"></p>
- <p ng-bind="EmployeeDetails.Salary"></p>
- <p ng-bind="EmployeeDetails.Age"></p>
- <p ng-bind="EmployeeDetails.MobileNumber"></p>
- <p ng-bind="EmployeeDetails.EmailId"></p>
- <p ng-bind="EmployeeDetails.FacebookId"></p>
- <p ng-bind="EmployeeDetails.TwitterId"></p>
To
- <p ng-bind="employeeDetails.firstName"></p>
- <p ng-bind="employeeDetails.middleName"></p>
- <p ng-bind="employeeDetails.lastName"></p>
- <p ng-bind="employeeDetails.currentDesignation"></p>
- <p ng-bind="employeeDetails.salary"></p>
- <p ng-bind="employeeDetails.age"></p>
- <p ng-bind="employeeDetails.mobileNumber"></p>
- <p ng-bind="employeeDetails.emailId"></p>
- <p ng-bind="employeeDetails.facebookId"></p>
- <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.
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
Vertical selection provides a lot more features but I am not going to explain all those features here.
- Converting Pascal Case to Camel Case: Using String class methods
- string txt = "WelcomeToCaseConversionLearning";
- 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.
- #region String Class Methods
-
- string txt = "WelcomeToCaseConversionLearning";
-
-
- WriteLine(txt.ToUpper());
-
-
- WriteLine(txt.ToLower());
- #endregion
-
- #region TextInfo Class Methods
-
- string txt2 = "welcomeTocaseconversionLEarning";
- TextInfo textinfo = new CultureInfo("en-US", false).TextInfo;
-
-
- WriteLine(textinfo.ToUpper(txt2));
-
-
- WriteLine(textinfo.ToLower(txt2));
-
-
- WriteLine(textinfo.ToTitleCase("welcome To CASe conversion LEArning"));
- #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.
- Convert a string to pascal case
- WriteLine("welcomeTo_CaseConversion_Learning".Pascalize());
-
- Convert a string to camel case
- WriteLine("WelcomeTo_Case_Conversion_Learning".Camelize());
-
- Convert a string to lower case.
- WriteLine(txt3.Humanize(LetterCasing.LowerCase));
-
- Convert a string to upper case.
- WriteLine(txt3.Humanize(LetterCasing.AllCaps));
-
- Convert a string to sentence case.
- WriteLine(txt3.Humanize(LetterCasing.Sentence));
-
- Convert a string to Title Case
- WriteLine(txt3.Humanize(LetterCasing.Title));
-
- Pluralize a string
- WriteLine("Employee".Pluralize());
-
- Singularize a string
- WriteLine("Employees".Singularize());
-
- Add Underscore
- WriteLine("Welcome to case conversion learning".Underscore());
-
- Convert underscore to dash or hyphen
- WriteLine("welcome_to_case_conversion_learning".Hyphenate());
- WriteLine("welcome_to_case_conversion_learning".Dasherize());
-
-
Apart from these examples, you can do hundreds of other types of conversion and formatting using Humanizer. For more details, visit here