views:

1363

answers:

6

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:

  • A way to turn "Lorem Lipsum Et" into "Lorem lipsum et"
  • A way to turn "Lorem lipsum et" into "Lorem Lipsum Et"
+2  A: 

Try this link:

Capitalization and Strings

miguel
isn't there something already built in C#?
marcgg
@Marc G hell no. .NET generally doesn't do anything "fuzzy" like PHP and RoR like to do.
Rex M
It's not fuzzy, it's awesome ^^ But ok, fair enought
marcgg
That does the same thing as a built in .NET function .ToTitleCase - capitalizing the first letter of each word in a string. Why borrow from the bathroom wall when you can do the same thing with a built in function?
patjbs
That link is to code that basically does the exact same thing as built in .NET .ToTitleCase method (which has advantages in that it tends to cover corner cases as well).
patjbs
Sorry for double post - wasn't updating for some reason :P
patjbs
A: 

If you just want to capitalize the first character, just stick this in a utility method of your own:

return string.IsNullOrEmpty(str) 
    ? str
    : str[0].ToUpperInvariant() + str.Substring(1).ToLowerInvariant();

There's also a library method to capitalize the first character of every word:

http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx

Daniel Earwicker
That'll just give you Title Case: "Lorem Ipsum Et", not sentence case: "Lorem ipsum et", right?
vinny
I think that does the opposite.
çağdaş
I was thinking this, but ToTitleCase is the opposite of what the OP wants.
Pat
And yet the accepted answer does the opposite...
Daniel Earwicker
my bad I thought it would work but it didnt. Titlecase seems to do the trick
marcgg
Uhhh... What's Titlecase? Do you mean ToTitleCase? It does the opposite of what you originally asked. Also, it's the very first thing I posted in my original answer. I give up.
Daniel Earwicker
+1  A: 

Far as I know, there's not a way to do that without writing (or cribbing) code. C# nets (ha!) you upper, lower and title (what you have) cases:

http://support.microsoft.com/kb/312890/EN-US/

vinny
+1  A: 

There is no prebuilt solution for proper linguistic captialization in .NET. What kind of capitialization are you going for? Are you following the Chicago Manual of Style conventions? AMA or MLA? Even plain english sentence capitalization has 1000's of special exceptions for words. I can't speak to what ruby's humanize does, but I imagine it likely doesn't follow linguistic rules of capitalization and instead does something much simpler.

Internally, we encountered this same issue and had to write a fairly large amount code just to handle proper (in our little world) casing of article titles, not even accounting for sentence capitalization. And it indeed does get "fuzzy" :)

It really depends on what you need - why are you trying to convert the sentences to proper capitalization (and in what context)?

patjbs
A: 

you can use simple css like this

text-transform:capitalize;

HeartDisk
+5  A: 

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 "NAMBLA" won't become "nambla" or "Nambla").

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

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
Spoike