Splitting CamelCase

前端 未结 15 2497
盖世英雄少女心
盖世英雄少女心 2020-12-07 10:56

This is all asp.net c#.

I have an enum

public enum ControlSelectionType 
{
    NotApplicable = 1,
    SingleSelectRadioButtons = 2,
    SingleSelectD         


        
15条回答
  •  一个人的身影
    2020-12-07 11:36

    This regex (^[a-z]+|[A-Z]+(?![a-z])|[A-Z][a-z]+) can be used to extract all words from the camelCase or PascalCase name. It also works with abbreviations anywhere inside the name.

    • MyHTTPServer will contain exactly 3 matches: My, HTTP, Server
    • myNewXMLFile will contain 4 matches: my, New, XML, File

    You could then join them into a single string using string.Join.

    string name = "myNewUIControl";
    string[] words = Regex.Matches(name, "(^[a-z]+|[A-Z]+(?![a-z])|[A-Z][a-z]+)")
        .OfType()
        .Select(m => m.Value)
        .ToArray();
    string result = string.Join(" ", words);
    

提交回复
热议问题