Skip to content

Instantly share code, notes, and snippets.

@shaosh
Created October 7, 2016 00:39
Show Gist options
  • Save shaosh/4886cb5722cf189e0c4f120e83086392 to your computer and use it in GitHub Desktop.
Save shaosh/4886cb5722cf189e0c4f120e83086392 to your computer and use it in GitHub Desktop.
C# code to convert underscore name to camelcase
private string underscoreToCamelCase(string name)
{
if (string.IsNullOrEmpty(name) || !name.Contains("_"))
{
return name;
}
string[] array = name.Split('_');
for(int i = 0; i < array.Length; i++)
{
string s = array[i];
string first = string.Empty;
string rest = string.Empty;
if (s.Length > 0)
{
first = Char.ToUpperInvariant(s[0]).ToString();
}
if (s.Length > 1)
{
rest = s.Substring(1).ToLowerInvariant();
}
array[i] = first + rest;
}
string newname = string.Join("", array);
if (newname.Length > 0)
{
newname = Char.ToLowerInvariant(newname[0]) + newname.Substring(1);
}
else
{
newname = name;
}
return newname;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment