Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否从枚举列表中删除下划线?_C# - Fatal编程技术网

C# 是否从枚举列表中删除下划线?

C# 是否从枚举列表中删除下划线?,c#,C#,我在组合框中使用国家的枚举。所有枚举都在一个名为Countries的类中。他们中的一些人有像美利坚合众国这样的下划线。我需要在组合框中显示之前删除这些下划线 我的想法是使用Replace(““,”),如果它是一个普通字符串,那么就很简单,但对于组合框来说就不那么简单了!因此,我想寻求一些帮助来解决这个问题?谢谢 private void InitializeGUI() { // Fill comboBox with countries cmbCountries.Items.Add

我在组合框中使用国家的枚举。所有枚举都在一个名为Countries的类中。他们中的一些人有像美利坚合众国这样的下划线。我需要在组合框中显示之前删除这些下划线

我的想法是使用
Replace(““,”)
,如果它是一个普通字符串,那么就很简单,但对于组合框来说就不那么简单了!因此,我想寻求一些帮助来解决这个问题?谢谢

private void InitializeGUI()
{
    // Fill comboBox with countries
    cmbCountries.Items.AddRange(Enum.GetNames(typeof(Countries))); 
}
使用Linq的功能:)

或者使用foreach:

private void InitializeGUI() 
{ 
    // Fill comboBox with countries 
    string[] countryNames = Enum.GetNames(typeof(Countries));
    foreach (string countryName in countryNames)
    {
        cmbCountries.Items.Add(countryName.Replace("_", " "));
    }
} 
使用Linq的功能:)

或者使用foreach:

private void InitializeGUI() 
{ 
    // Fill comboBox with countries 
    string[] countryNames = Enum.GetNames(typeof(Countries));
    foreach (string countryName in countryNames)
    {
        cmbCountries.Items.Add(countryName.Replace("_", " "));
    }
} 
这将创建一个
IEnumerable
,其中包含您从
Enum
中选择的名称(做得好,使用
Enum
对我来说总是很糟糕),然后替换每个名称的下划线

你也可以这样写:

countryNames = from country
               in Enum.GetNames(typeof(Countries))
               select country.Replace("_", " ");
cmbCountries.Items.AddRange(countryNames);
这将创建一个
IEnumerable
,其中包含您从
Enum
中选择的名称(做得好,使用
Enum
对我来说总是很糟糕),然后替换每个名称的下划线

你也可以这样写:

countryNames = from country
               in Enum.GetNames(typeof(Countries))
               select country.Replace("_", " ");
cmbCountries.Items.AddRange(countryNames);

+1.最好“打断”长线,这样恼人的滚动条就不会出现在这里了。希望你不要生气,如果你想的话可以回滚。嗯,我在我的课上没有和LINQ一起工作,我们希望在下一节课上使用它。可以用其他方式吗?更基本一点?更新了没有LINQgdoron的示例,您能给我一些关于如何使用foreach完成此任务的提示吗?Duncan,我在使用您的代码时收到一条红线?有什么问题吗?+1。最好是“打断”长线,这样恼人的滚动条就不会出现在这里了。希望你不要生气,如果你想的话可以回滚。嗯,我在我的课上没有和LINQ一起工作,我们希望在下一节课上使用它。可以用其他方式吗?更基本一点?更新了没有LINQgdoron的示例,您能给我一些关于如何使用foreach完成此任务的提示吗?Duncan,我在使用您的代码时收到一条红线?有什么不对劲吗?
Enum.GetNames(typeof(Countries)).Select(x => x.Replace("_", " "));