Skip to content

Instantly share code, notes, and snippets.

@jesuslpm
Created May 4, 2024 07:53
Show Gist options
  • Save jesuslpm/6effa6af4b7ef620a10dc92162ae23c0 to your computer and use it in GitHub Desktop.
Save jesuslpm/6effa6af4b7ef620a10dc92162ae23c0 to your computer and use it in GitHub Desktop.
From Kebab to Camel
using System.Diagnostics;
using System.Text;
namespace Kebab
{
internal class Program
{
static void Main(string[] args)
{
var test1 = FromKebabToCamel("kebab-case-two");
Debug.Assert(test1 == "kebabCaseTwo");
var test2 = FromKebabToCamel("kebab-case");
Debug.Assert(test2 == "kebabCase");
var test3 = FromKebabToCamel("kebab");
Debug.Assert(test3 == "kebab");
var test4 = FromKebabToCamel("kebab-case-");
Debug.Assert(test4 == "kebabCase");
var test5 = FromKebabToCamel("kebab-case--");
Debug.Assert(test5 == "kebabCase");
var test6 = FromKebabToCamel("-extrange-kebab");
Debug.Assert(test6 == "extrangeKebab");
Console.WriteLine("All tests passed");
}
public static string FromKebabToCamel(string identifier)
{
identifier = identifier.Trim('-');
var stringArray = identifier.Split('-');
var output = new StringBuilder(stringArray[0]);
for (var i = 1; i < stringArray.Length; i++)
{
var token = stringArray[i];
if (token.Length > 0)
{
output.Append(char.ToUpper(token[0]) +
token.Substring(1));
}
}
return output.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment