gpt4 book ai didi

c# - 将枚举转换为人类可读的值

转载 作者:IT王子 更新时间:2023-10-29 04:21:42 26 4
gpt4 key购买 nike

有谁知道如何将枚举值转换为人类可读的值?

例如:

ThisIsValueA should be "This is Value A".

最佳答案

从某个 Ian Horwill 留在 blog post long ago 的 vb 代码片段中转换它...我已经在生产中成功地使用了它。

    /// <summary>
/// Add spaces to separate the capitalized words in the string,
/// i.e. insert a space before each uppercase letter that is
/// either preceded by a lowercase letter or followed by a
/// lowercase letter (but not for the first char in string).
/// This keeps groups of uppercase letters - e.g. acronyms - together.
/// </summary>
/// <param name="pascalCaseString">A string in PascalCase</param>
/// <returns></returns>
public static string Wordify(string pascalCaseString)
{
Regex r = new Regex("(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])");
return r.Replace(pascalCaseString, " ${x}");
}

(要求,“使用 System.Text.RegularExpressions;”)

因此:

Console.WriteLine(Wordify(ThisIsValueA.ToString()));

会回来的,

"This Is Value A".

它比提供 Description 属性更简单,也更少冗余。

仅当您需要提供一个间接层(问题没有要求)时,属性在这里才有用。

关于c# - 将枚举转换为人类可读的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com