More performative option, flexible, integrated and more correct according to the criterion that if you already have one tab, you should not put another one:
using static System.Console;
using System.Text;
public static class Program {
public static void Main() {
WriteLine("CamelCase".SplitCamelCase());
WriteLine("Camel-Case".SplitCamelCase());
WriteLine("Camel---Case".SplitCamelCase());
WriteLine("Camel.Case".SplitCamelCase());
WriteLine("Ca".SplitCamelCase());
WriteLine("aC".SplitCamelCase());
WriteLine("CC".SplitCamelCase());
WriteLine("C".SplitCamelCase());
WriteLine("CamelCaseC".SplitCamelCase());
WriteLine("".SplitCamelCase());
WriteLine("pascalCase".SplitCamelCase());
WriteLine("CamelCase".SplitCamelCase('-', ""));
WriteLine("Camel-Case".SplitCamelCase('-', ""));
WriteLine("Camel---Case".SplitCamelCase('-', ""));
WriteLine("Camel.Case".SplitCamelCase('-', ""));
WriteLine("Ca".SplitCamelCase('-', ""));
WriteLine("aC".SplitCamelCase('-', ""));
WriteLine("CC".SplitCamelCase('-', ""));
WriteLine("C".SplitCamelCase('-', ""));
WriteLine("CamelCaseC".SplitCamelCase('-', ""));
WriteLine("".SplitCamelCase('-', ""));
WriteLine("pascalCase".SplitCamelCase('-', ""));
}
public static string SplitCamelCase(this string text, char separator = '-', string separators = "-=_+!@#$%&*()'^~[]{}/?;:.,<>|\\"") {
if (string.IsNullOrEmpty(text) || text.Length < 2) return text;
var sb = new StringBuilder(text.Length + text.Length / 3);
for (var i = 0; i < text.Length; i++) {
if (char.IsUpper(text[i]) && i > 0 && !separators.Contains(text[i - 1].ToString())) sb.Append(separator);
sb.Append(text[i]);
}
return sb.ToString();
}
}
See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .
It does not matter if the string source comes from a enum
, you do in the text, it does not enum
. And getting the string of enum
has already proven to be able to do.