I have a string "THURSDAY 26th JANUARY 2011".
When I format this using CultureInfo.ToTitleCase():
var dateString = "THURSDAY 26th JANUARY 2011";
var titleString = myCultureInfoObject.TextInfo.ToTitleCase(dateString);
It is displayed like this: "Thursday 26Th January 2011"
. This is exactly what I need...except the T in 26Th has been capitalised. Is there any way to stop this from happening as it is a date and looks wrong? I.e only title-casing characters that don't have a number directly before them?
You could use a regex with a MatchEvaluator
to put only "real" words in title case:
var dateString = "THURSDAY 26th JANUARY 2011";
MatchEvaluator ev = m => myCultureInfoObject.TextInfo.ToTitleCase(m.Value);
var titleString = Regex.Replace(dateString, @"\b[a-zA-Z]+\b", ev);
This will apply title case only to "THURSDAY" and "JANUARY", but not "26TH" because it doesn't match the regex pattern.
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