How to set space on Enum

后端 未结 14 1485
栀梦
栀梦 2020-11-29 08:00

I want to set the space on my enum. Here is my code sample:

public enum category
{
    goodBoy=1,
    BadBoy
}

I want to set



        
14条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 08:30

    I used Regex to split the values by capital letter and then immediately join into a string with a space between each string in the returned array.

    string.Join(" ", Regex.Split(v.ToString(), @"(?

    First get the values of the enum:

    var values = Enum.GetValues(typeof(Category));
    

    Then loop through the values and use the code above to get the values:

    var ret = new Dictionary();
    
    foreach (Category v in values)
    {
       ret.Add((int)v, string.Join(" ", Regex.Split(v.ToString(), @"(?

    In my case I needed a dictionary with the value and display name so that it why I have the variable "ret"

提交回复
热议问题