I'm tying to write a handy little extension method that will take a string and return it formatted as if it was a lowerCamelCase JSON identifier.
This is what I have so far.. please can I get help improving it?
I need the following behaviour:
- UserName => userName
- UserID => userId
- user => user
- fishing trip => fishingTrip
- 123123123 => 123123123
- aaa123 => aaa123
- hello world this is me => helloWorldThisIsMe
Bonus - if we could somehow strip out non [A-Za-z0-9] characters? I guess i could just do another round of regex?
Thanks for any help!
public static string ToJsonIdentifier(string s)
{
// special case, s is empty
if (string.IsNullOrEmpty(s)) return s;
// clean up the string, remove any non-standard chars
s = Regex.Replace(s, @"[^A-Za-z0-9\s]+", "");
// special case s is whitespace
if (string.IsNullOrWhiteSpace(s)) return String.Empty;
// special case s is only 1 letter
if (!string.IsNullOrEmpty(s) && s.Length == 1)
return s.ToLowerInvariant();
// detect word boundaries where the case changes and add whitespace there, so the next code splits it up
s = Regex.Replace(s, "([a-z])([A-Z])", m=> m.Groups[1].Value + " " + m.Groups[2].Value);
// multiple words, so for each whitespace separated bit, uppercase the first letter, and deal with special cases
if (s.Contains(" "))
{
s = string.Join("", s.Split(' ').ToList().Select(z =>
{
if (string.IsNullOrWhiteSpace(z)) return string.Empty;
if (z.Length == 1) return z.ToUpperInvariant();
return z.ToUpperInvariant()
.Substring(0, 1) + z.Substring(1).ToLowerInvariant();
}));
}
// lowercase the first letter
return char.ToLower(s[0]) + s.Substring(1);
}
Research: I've seen these questions, which seem related:
My ongoing attempt: https://dotnetfiddle.net/PR31Hl