Created
March 12, 2025 00:42
-
-
Save santisq/881a134da28f503638c5056230138fa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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