How to separate "words" in CamelCase in C #?

12

How can I separate "words" into CamelCase using hyphen for example?

With a string:

string example = "CamelCase";
// CamelCase para:
// Camel-Case

and enumerator:

enum Example {
    CamelCase
}

Example.CamelCase.ToString();
// CamelCase para:
// Camel-Case
    
asked by anonymous 24.02.2018 / 15:37

5 answers

13

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.

    
24.02.2018 / 15:55
11

I did based in this answer , it does not use regular expression see:

string camelCase = string.Concat(ExampleEnum.CamelCase.ToString().Select((x,i) => i > 0 && char.IsUpper(x) ? "-" + x.ToString() : x.ToString())); 

Output:

  

Camel-Case

See working at .NET Fiddle .

    
24.02.2018 / 15:49
7

I made the following code, it is not as simple as the cat's, but it can be more didactic. I hope it helps:

public static void Main()
{
    string camelCase = "CamelCase";
    string newCamelCase= HiffenCamelCase(camelCase);


    Console.WriteLine(camelCase);
    Console.WriteLine(newCamelCase);

}

static string HiffenCamelCase(string s)
{
    if (!String.IsNullOrEmpty(s))
    {
        string result = s[0].ToString();
        for (int i = 1; i < s.Length;i++)
        {
            result += s[i]+ (i+1<s.Length ? (Char.IsUpper(s[i+1]) ?"-" : "") : "");
        }

        return result;
    }
    else return s;
}

Output:

  

CamelCase

     

Camel-Case

I put it in .NETFiddle: link

    
24.02.2018 / 15:53
6

Using regular expressions we can for example:

using System.Text.RegularExpressions;
...
id = Regex.Replace(id,"(?<=[a-z])(?=[A-Z])","-");

A complete example could be:

using static System.Console;
using System.Text.RegularExpressions;

public static class Program {
  public static void Main() {
    WriteLine("CamelCase".SplitCamelCase());  }
  public static string SplitCamelCase(this string id){
    return Regex.Replace(id,"(?<=[a-z])(?=[A-Z])","-"); }
}

Of course, it adapts the expressions to the desired semantics. For example if we want case sequences to be separated, ..."(?<=[A-Za-z])(?=[A-Z])"...

    
24.02.2018 / 18:06
1

It can be this way too:

string camelCaseString = "EuSouUmaStringCamelCase";
string retorno = System.Text.RegularExpressions.Regex.Replace(
    camelCaseString,
    "(?<=[a-z])([A-Z])",
    "-$1",
    System.Text.RegularExpressions.RegexOptions.Compiled).Trim();

Console.Write(retorno);
    
02.04.2018 / 21:12