Skip to content

Instantly share code, notes, and snippets.

@santisq
Created March 12, 2025 00:42
Show Gist options
  • Save santisq/881a134da28f503638c5056230138fa7 to your computer and use it in GitHub Desktop.
Save santisq/881a134da28f503638c5056230138fa7 to your computer and use it in GitHub Desktop.
static unsafe void ToTitleCase(string input)
{
if (input.Length == 0)
{
return;
}
fixed (char* chars = input)
{
bool startOrNotLetter = true;
for (int i = 0; i < input.Length; i++)
{
char current = chars[i];
if (startOrNotLetter)
{
startOrNotLetter = false;
if (char.IsLower(current))
{
chars[i] = char.ToUpperInvariant(current);
}
continue;
}
if (char.IsUpper(current))
{
chars[i] = char.ToLowerInvariant(current);
continue;
}
startOrNotLetter = !char.IsLetter(current);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment