How can I convert text to Pascal case?

wlz picture wlz · Sep 5, 2013 · Viewed 52.9k times · Source

I have a variable name, say "WARD_VS_VITAL_SIGNS", and I want to convert it to Pascal case format: "WardVsVitalSigns"

WARD_VS_VITAL_SIGNS -> WardVsVitalSigns

How can I make this conversion?

Answer

Nilesh picture Nilesh · Sep 5, 2013

You do not need a regular expression for that.

var yourString = "WARD_VS_VITAL_SIGNS".ToLower().Replace("_", " ");
TextInfo info = CultureInfo.CurrentCulture.TextInfo;
yourString = info.ToTitleCase(yourString).Replace(" ", string.Empty);
Console.WriteLine(yourString);