这是all asp.net c#。
我有一个枚举
public enum ControlSelectionType
{
NotApplicable = 1,
SingleSelectRadioButtons = 2,
SingleSelectDropDownList = 3,
MultiSelectCheckBox = 4,
MultiSelectListBox = 5
}它的数值存储在我的数据库中。我在一个datagrid中显示这个值。
<asp:boundcolumn datafield="ControlSelectionTypeId" headertext="Control Type"></asp:boundcolumn>ID对用户没有任何意义,因此我使用以下内容将边界列更改为模板列。
<asp:TemplateColumn>
<ItemTemplate>
<%# Enum.Parse(typeof(ControlSelectionType), DataBinder.Eval(Container.DataItem, "ControlSelectionTypeId").ToString()).ToString()%>
</ItemTemplate>
</asp:TemplateColumn>这样好多了..。但是,如果有一个简单的函数,我可以在枚举周围放一个简单的函数,将它按驼峰大小写拆分,这样单词就可以在数据网格中很好地包装起来。
注意:我完全意识到有更好的方法可以做到这一点。这个屏幕纯粹是内部使用的,我只是想要一个快速的黑客在地方,以显示它更好一点。
发布于 2009-04-21 16:01:42
实际上,正则表达式/替换是另一个答案中描述的方法,但是,如果您想走不同的方向,这也可能对您有用
using System.ComponentModel;
using System.Reflection;..。
public static string GetDescription(System.Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}这将允许您将枚举定义为
public enum ControlSelectionType
{
[Description("Not Applicable")]
NotApplicable = 1,
[Description("Single Select Radio Buttons")]
SingleSelectRadioButtons = 2,
[Description("Completely Different Display Text")]
SingleSelectDropDownList = 3,
}摘自
http://www.codeguru.com/forum/archive/index.php/t-412868.html
发布于 2011-05-26 19:41:48
我使用:
public static string SplitCamelCase(string input)
{
return System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
}摘自http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx
vb.net:
Public Shared Function SplitCamelCase(ByVal input As String) As String
Return System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim()
End Function发布于 2016-05-31 02:58:30
此正则表达式(^[a-z]+|[A-Z]+(?![a-z])|[A-Z][a-z]+)可用于从camelCase或PascalCase名称中提取所有单词。它还可以在名称中的任何位置使用缩写。
MyHTTPServer将包含3个匹配项:ServermyNewXMLFile将包含4个匹配项:my,New,XML,My,HTTP,my然后,您可以使用string.Join将它们连接成一个字符串。
string name = "myNewUIControl";
string[] words = Regex.Matches(name, "(^[a-z]+|[A-Z]+(?![a-z])|[A-Z][a-z]+)")
.OfType<Match>()
.Select(m => m.Value)
.ToArray();
string result = string.Join(" ", words);正如@DanielB在评论中指出的,正则表达式不适用于数字(和下划线),所以这里有一个改进版本,支持任何带有单词、首字母缩写、数字、下划线的标识符(略有修改的@JoeJohnston版本),请参阅online demo (fiddle)
([A-Z]+(?![a-z])|[A-Z][a-z]+|[0-9]+|[a-z]+)极端示例:__snake_case12_camelCase_TLA1ABC snake,case,12,camel,Case,TLA,1,ABC,→
https://stackoverflow.com/questions/773303
复制相似问题