I could write my own algorithm to do it, but I feel there should be the equivalent to ruby's humanize in C#.
I googled it but only found ways to humanize dates.
Examples:
To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it. Now, we would get the remaining characters of the string using the slice() function.
Today we will learn how to code a C program on how to Capitalize first and last letter of each word of a string i.e. to convert first and last letter of each word into uppercase . We will use toupper() function in the process. The toupper() function is used to convert lowercase alphabet to uppercase.
As discussed in the comments of @miguel's answer, you can use TextInfo.ToTitleCase
which has been available since .NET 1.1. Here is some code corresponding to your example:
string lipsum1 = "Lorem lipsum et"; // Creates a TextInfo based on the "en-US" culture. TextInfo textInfo = new CultureInfo("en-US",false).TextInfo; // Changes a string to titlecase. Console.WriteLine("\"{0}\" to titlecase: {1}", lipsum1, textInfo.ToTitleCase( lipsum1 )); // Will output: "Lorem lipsum et" to titlecase: Lorem Lipsum Et
It will ignore casing things that are all caps such as "LOREM LIPSUM ET" because it is taking care of cases if acronyms are in text so that "IEEE" (Institute of Electrical and Electronics Engineers) won't become "ieee" or "Ieee".
However if you only want to capitalize the first character you can do the solution that is over here… or you could just split the string and capitalize the first one in the list:
string lipsum2 = "Lorem Lipsum Et"; string lipsum2lower = textInfo.ToLower(lipsum2); string[] lipsum2split = lipsum2lower.Split(' '); bool first = true; foreach (string s in lipsum2split) { if (first) { Console.Write("{0} ", textInfo.ToTitleCase(s)); first = false; } else { Console.Write("{0} ", s); } } // Will output: Lorem lipsum et
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With