Rob*_*Day 72 c# asp.net string
这都是asp.net c#.
我有一个枚举
public enum ControlSelectionType
{
NotApplicable = 1,
SingleSelectRadioButtons = 2,
SingleSelectDropDownList = 3,
MultiSelectCheckBox = 4,
MultiSelectListBox = 5
}
Run Code Online (Sandbox Code Playgroud)
它的数值存储在我的数据库中.我在数据网格中显示此值.
<asp:boundcolumn datafield="ControlSelectionTypeId" headertext="Control Type"></asp:boundcolumn>
Run Code Online (Sandbox Code Playgroud)
ID对用户没有任何意义,因此我已使用以下内容将boundcolumn更改为模板列.
<asp:TemplateColumn>
<ItemTemplate>
<%# Enum.Parse(typeof(ControlSelectionType), DataBinder.Eval(Container.DataItem, "ControlSelectionTypeId").ToString()).ToString()%>
</ItemTemplate>
</asp:TemplateColumn>
Run Code Online (Sandbox Code Playgroud)
这样做要好得多......但是,如果有一个简单的函数我可以放在Enum周围,通过Camel案例将它拆分,以便在数据网格中很好地包装它.
注意:我完全清楚有更好的方法可以做到这一切.这个屏幕纯粹是在内部使用的,我只想快速入侵以便更好地显示它.
Til*_*ito 113
我用了:
public static string SplitCamelCase(string input)
{
return System.Text.RegularExpressions.Regex.Replace(input, "([A-Z])", " $1", System.Text.RegularExpressions.RegexOptions.Compiled).Trim();
}
Run Code Online (Sandbox Code Playgroud)
摘自http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx
Eoi*_*ell 73
事实上,正如另一个答案中所描述的那样,正则表达式/替换是另一种方式,但如果您想要朝着不同的方向发展,这对您也有用
using System.ComponentModel;
using System.Reflection;
Run Code Online (Sandbox Code Playgroud)
...
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();
}
Run Code Online (Sandbox Code Playgroud)
这将允许您将您的枚举定义为
public enum ControlSelectionType
{
[Description("Not Applicable")]
NotApplicable = 1,
[Description("Single Select Radio Buttons")]
SingleSelectRadioButtons = 2,
[Description("Completely Different Display Text")]
SingleSelectDropDownList = 3,
}
Run Code Online (Sandbox Code Playgroud)
取自
http://www.codeguru.com/forum/archive/index.php/t-412868.html
Gho*_*Man 22
此正则表达式(^[a-z]+|[A-Z]+(?![a-z])|[A-Z][a-z]+)可用于从camelCase或PascalCase名称中提取所有单词.它也适用于名称中任何位置的缩写.
MyHTTPServer将仅包含3场比赛:My,HTTP,ServermyNewXMLFile将包含4场比赛:my,New,XML,File然后,您可以使用它们将它们连接成一个字符串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);
Run Code Online (Sandbox Code Playgroud)
em7*_*m70 14
如果C#3.0是一个选项,您可以使用以下单行来完成工作:
Regex.Matches(YOUR_ENUM_VALUE_NAME, "[A-Z][a-z]+").OfType<Match>().Select(match => match.Value).Aggregate((acc, b) => acc + " " + b).TrimStart(' ');
Run Code Online (Sandbox Code Playgroud)
Pet*_*cio 14
Tillito的答案不能处理已经包含空格的字符串或缩略语.这解决了它:
public static string SplitCamelCase(string input)
{
return Regex.Replace(input, "(?<=[a-z])([A-Z])", " $1", RegexOptions.Compiled);
}
Run Code Online (Sandbox Code Playgroud)
这是一个可以灵活处理数字和多个大写字符的扩展方法,并且还允许在最终字符串中使用上层特定的首字母缩略词:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web.Configuration;
namespace System
{
/// <summary>
/// Extension methods for the string data type
/// </summary>
public static class ConventionBasedFormattingExtensions
{
/// <summary>
/// Turn CamelCaseText into Camel Case Text.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <remarks>Use AppSettings["SplitCamelCase_AllCapsWords"] to specify a comma-delimited list of words that should be ALL CAPS after split</remarks>
/// <example>
/// wordWordIDWord1WordWORDWord32Word2
/// Word Word ID Word 1 Word WORD Word 32 Word 2
///
/// wordWordIDWord1WordWORDWord32WordID2ID
/// Word Word ID Word 1 Word WORD Word 32 Word ID 2 ID
///
/// WordWordIDWord1WordWORDWord32Word2Aa
/// Word Word ID Word 1 Word WORD Word 32 Word 2 Aa
///
/// wordWordIDWord1WordWORDWord32Word2A
/// Word Word ID Word 1 Word WORD Word 32 Word 2 A
/// </example>
public static string SplitCamelCase(this string input)
{
if (input == null) return null;
if (string.IsNullOrWhiteSpace(input)) return "";
var separated = input;
separated = SplitCamelCaseRegex.Replace(separated, @" $1").Trim();
//Set ALL CAPS words
if (_SplitCamelCase_AllCapsWords.Any())
foreach (var word in _SplitCamelCase_AllCapsWords)
separated = SplitCamelCase_AllCapsWords_Regexes[word].Replace(separated, word.ToUpper());
//Capitalize first letter
var firstChar = separated.First(); //NullOrWhiteSpace handled earlier
if (char.IsLower(firstChar))
separated = char.ToUpper(firstChar) + separated.Substring(1);
return separated;
}
private static readonly Regex SplitCamelCaseRegex = new Regex(@"
(
(?<=[a-z])[A-Z0-9] (?# lower-to-other boundaries )
|
(?<=[0-9])[a-zA-Z] (?# number-to-other boundaries )
|
(?<=[A-Z])[0-9] (?# cap-to-number boundaries; handles a specific issue with the next condition )
|
(?<=[A-Z])[A-Z](?=[a-z]) (?# handles longer strings of caps like ID or CMS by splitting off the last capital )
)"
, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace
);
private static readonly string[] _SplitCamelCase_AllCapsWords =
(WebConfigurationManager.AppSettings["SplitCamelCase_AllCapsWords"] ?? "")
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(a => a.ToLowerInvariant().Trim())
.ToArray()
;
private static Dictionary<string, Regex> _SplitCamelCase_AllCapsWords_Regexes;
private static Dictionary<string, Regex> SplitCamelCase_AllCapsWords_Regexes
{
get
{
if (_SplitCamelCase_AllCapsWords_Regexes == null)
{
_SplitCamelCase_AllCapsWords_Regexes = new Dictionary<string,Regex>();
foreach(var word in _SplitCamelCase_AllCapsWords)
_SplitCamelCase_AllCapsWords_Regexes.Add(word, new Regex(@"\b" + word + @"\b", RegexOptions.Compiled | RegexOptions.IgnoreCase));
}
return _SplitCamelCase_AllCapsWords_Regexes;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用C#扩展方法
public static string SpacesFromCamel(this string value)
{
if (value.Length > 0)
{
var result = new List<char>();
char[] array = value.ToCharArray();
foreach (var item in array)
{
if (char.IsUpper(item) && result.Count > 0)
{
result.Add(' ');
}
result.Add(item);
}
return new string(result.ToArray());
}
return value;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像
var result = "TestString".SpacesFromCamel();
Run Code Online (Sandbox Code Playgroud)
结果将是
测试字符串
| 归档时间: |
|
| 查看次数: |
37955 次 |
| 最近记录: |