You can roll your own by using static methods of the char
class.
The idea is to only capitalize characters that are preceded by a non letter character. This is a naive way of approaching this, but fun to tinker around with.
var input = "TIM JAMES";
var output = "";
var thisChar = ' ';
var shouldCapitalize = true;
for (int i = 0; i < input.Length; i++)
{
thisChar = input[i];
if (!char.IsLetter(thisChar))
{
shouldCapitalize = true;
}
else
{
if (shouldCapitalize)
{
thisChar = char.ToUpper(thisChar);
shouldCapitalize = false;
}
else
{
thisChar = char.ToLower(thisChar);
}
}
output += thisChar;
}
Console.WriteLine("Hello there, " + output);
To handle Mc names, eg. McDonuts, you can do the following.
private bool IsMcName(string output, int index, char currentChar)
{
return
index > 0 &&
char.ToLower(currentChar) == 'c' &&
output[index - 1] == 'M';
}
and call this function from the else statement where the current char is converted to lower case.
else
{
if (IsMcName(output, i, thisChar))
{
shouldCapitalize = true;
}
thisChar = char.ToLower(thisChar);
}
I made a similar function for Mac names, eg MacDonuts, and it works.. however I believe it would interfere more times than help. eg. Macy becomes MacY.
private bool IsMacName(string output, int index, char currentChar)
{
return
index > 1 &&
char.ToLower(currentChar) == 'c' &&
output[index - 1] == 'a' &&
output[index - 2] == 'M';
}
This can be used in-place, or in-conjunction with, the McName function. eg. if (McName(..) || MacName(..))