I have a string of text (about 5-6 words mostly) that I need to convert.
Currently the text looks like:
THIS IS MY TEXT RIGHT NOW
I
There's a couple of ways to go about converting the first char of a string to upper case.
The first way is to create a method that simply caps the first char and appends the rest of the string using a substring:
public string UppercaseFirst(string s)
{
return char.ToUpper(s[0]) + s.Substring(1);
}
The second way (which is slightly faster) is to split the string into a char array and then re-build the string:
public string UppercaseFirst(string s)
{
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
When building big tables speed is a concern so Jamie Dixon's second function is best, but it doesn't completely work as is...
It fails to take all of the letters to lowercase, and it only capitalizes the first letter of the string, not the first letter of each word in the string... the below option fixes both issues:
public string UppercaseFirstEach(string s)
{
char[] a = s.ToLower().ToCharArray();
for (int i = 0; i < a.Count(); i++ )
{
a[i] = i == 0 || a[i-1] == ' ' ? char.ToUpper(a[i]) : a[i];
}
return new string(a);
}
Although at this point, whether this is still the fastest option is uncertain, the Regex
solution provided by George Mauer might be faster... someone who cares enough should test it.
I probably prefer to invoke the ToTitleCase from CultureInfo (System.Globalization) than Thread.CurrentThread (System.Threading)
string s = "THIS IS MY TEXT RIGHT NOW";
s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());
but it should be the same as jspcal solution
EDIT
Actually those solutions are not the same: CurrentThread
--calls--> CultureInfo
!
System.Threading.Thread.CurrentThread.CurrentCulture
string s = "THIS IS MY TEXT RIGHT NOW";
s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());
IL_0000: ldstr "THIS IS MY TEXT RIGHT NOW"
IL_0005: stloc.0 // s
IL_0006: call System.Threading.Thread.get_CurrentThread
IL_000B: callvirt System.Threading.Thread.get_CurrentCulture
IL_0010: callvirt System.Globalization.CultureInfo.get_TextInfo
IL_0015: ldloc.0 // s
IL_0016: callvirt System.String.ToLower
IL_001B: callvirt System.Globalization.TextInfo.ToTitleCase
IL_0020: stloc.0 // s
System.Globalization.CultureInfo.CurrentCulture
string s = "THIS IS MY TEXT RIGHT NOW";
s = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower());
IL_0000: ldstr "THIS IS MY TEXT RIGHT NOW"
IL_0005: stloc.0 // s
IL_0006: call System.Globalization.CultureInfo.get_CurrentCulture
IL_000B: callvirt System.Globalization.CultureInfo.get_TextInfo
IL_0010: ldloc.0 // s
IL_0011: callvirt System.String.ToLower
IL_0016: callvirt System.Globalization.TextInfo.ToTitleCase
IL_001B: stloc.0 // s
References:
Untested but something like this should work:
var phrase = "THIS IS MY TEXT RIGHT NOW";
var rx = new System.Text.RegularExpressions.Regex(@"(?<=\w)\w");
var newString = rx.Replace(phrase,new MatchEvaluator(m=>m.Value.ToLowerInvariant()));
Essentially it says "preform a regex match on all occurrences of an alphanumeric character that follows another alphanumeric character and then replace it with a lowercase version of itself"
If you're using on a web page, you can also use CSS:
style="text-transform:capitalize;"