Created
February 8, 2018 18:02
-
-
Save Danielkaas94/68ecd5123f2b074952867fe630f52c2c to your computer and use it in GitHub Desktop.
ToCamelCase & ToCamelCase2
This file contains 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
/// <summary> | |
/// Converts dash/underscore delimited words into camel casing. | |
/// The first word within the output should be capitalized only if the original word was capitalized. | |
/// </summary> | |
/// <param name="str"></param> | |
/// <returns></returns> | |
public static string ToCamelCase(string str) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
string tempStr = str; | |
char lastChar = 'a'; | |
foreach (char character in str) | |
{ | |
if (character == '-' || character == '_') | |
{ | |
} | |
else | |
{ | |
if (lastChar == '_' || lastChar == '-') | |
{ | |
sb.Append(character.ToString().ToUpper()); | |
} | |
else | |
{ | |
sb.Append(character); | |
} | |
} | |
lastChar = character; | |
} | |
return sb.ToString(); | |
} |
This file contains 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
/// <summary> | |
/// Same as ToCamelCase, just in one line. | |
/// </summary> | |
/// <param name="str"></param> | |
/// <returns></returns> | |
public static string ToCamelCase2(string str) | |
{ | |
return Regex.Replace(str, @"[_-](\w)", m => m.Groups[1].Value.ToUpper()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment