Capitalize First Letter of Every Word (C#)
I hate non uniform data, I really do. It’s one of those things that a client will enter into the system a thousand times incorrectly with no rhyme or reason. It is also followed by the eventual, can you fix these million records so they are correct. Ugh! What?!? Ok, so you can fix it in your database or create code at display time to fix it. I always opt for the second, since you can’t trust your users to stop doing what they have been doing. So here is code to nicely display peoples names, cities, or whatever needs to the first letter capitalized.
public static string CapitalizeFirst(this string input)
{
if (string.IsNullOrEmpty(input)) {
return input;
}
var words = input.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries);
var result = words.Select(w => w[0].ToString().ToUpper() + w.ToLower().Remove(0, 1)).Aggregate((s,w) => s + ' ' + w );
return result.Trim();
}
Not rocket science, I know but still equally helpful.
-
Anil Patil
-
http://www.Marisic.Net dotnetchris
-
http://www.Marisic.Net dotnetchris
-
AquaBirdConsult
-
http://www.Marisic.Net dotnetchris
