How to make a dropdown list of all cultures (but no repeats)

How to make a dropdown list of all cultures (but no repeats)

I'm trying to make 2 dropdown lists.

The top one offers all cultures, (but no repeats). Example: English, Spanish, Filipino

After selecting from the top list the bottom list will show any specific types.

I right now I use this code for my top list.

foreach (CultureInfo cultureInfo in CultureInfo.GetCultures(CultureTypes.NeutralCultures))

However it does not show Filipino (Philippines) I'd rather not use GetCultures(CultureTypes.AllCultures)) because it shows too many at once.

It seems like I may need to load NeutralCultures into an IList. Then iterate through AllCultures to make sure it's ThreeLetterISOLanguageName is in the list, if not add it.

There a best practice for this?

Thanks

 

回答1

Look at the reference for the different CultureTypes values. It tells you what is included for each.

I guess you want everything that's in all but the specific cultures? You could either combine all non-specific cultures into a set or get all cultures and exclude the specific ones. The second approach would be easiest to express in LINQ:

var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
                          .Except(CultureInfo.GetCultures(CultureTypes.SpecificCultures));

Though it seems that since CultureTypes has the flags attribute, we could also just mask out the SpecificCultures when getting them.

var cultures = CultureInfo.GetCultures(
    CultureTypes.AllCultures & ~CultureTypes.SpecificCultures
);

 

@aron: I'm not sure I understand what you are saying. The first part of the name is just the ISO 639-1 code code while the second part is the ISO 3166 code. ISO 639-1 code alone refers to a language (a neutral culture not associated with a specific region). Though the Czech Republic (cs-CZ) uses the the Czech language (cs), they are not equivalent. Both of those are specific cultures and should not be listed here. If it is, it is probably a culture object that was created by you (which is included). May 11, 2011 at 3:22
 
 

Bitwise and shift operators (C# reference)

The following operators perform bitwise or shift operations with operands of the integral numeric types or the char type:

Those operators are defined for the int, uint, long, and ulong types. When both operands are of other integral types (sbyte, byte, short, ushort, or char), their values are converted to the int type, which is also the result type of an operation. When operands are of different integral types, their values are converted to the closest containing integral type. For more information, see the Numeric promotions section of the C# language specification. The compound operators (such as >>=) don't convert their arguments to int or have the result type as int.

The &, |, and ^ operators are also defined for operands of the bool type. For more information, see Boolean logical operators.

Bitwise and shift operations never cause overflow and produce the same results in checked and unchecked contexts.

 

Bitwise complement operator ~

The ~ operator produces a bitwise complement of its operand by reversing each bit:

C#
uint a = 0b_0000_1111_0000_1111_0000_1111_0000_1100;
uint b = ~a;
Console.WriteLine(Convert.ToString(b, toBase: 2));
// Output:
// 11110000111100001111000011110011

You can also use the ~ symbol to declare finalizers. For more information, see Finalizers.

 

Convert an integer to a binary string with leading zeros

I need to convert int to bin and with extra bits.

string aaa = Convert.ToString(3, 2);

it returns 11, but I need 0011, or 00000011.

How is it done?

 

回答1

11 is binary representation of 3. The binary representation of this value is 2 bits.

3 = 20 * 1 + 21 * 1

You can use String.PadLeft(Int, Char) method to add these zeros.

// convert number 3 to binary string. 
// And pad '0' to the left until string will be not less then 4 characters
Convert.ToString(3, 2).PadLeft(4, '0') // 0011
Convert.ToString(3, 2).PadLeft(8, '0') // 00000011
 
 
 
 
 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-09-20 Powershell 常见问题
2018-09-20 touch all contents in a folder recursively
2018-09-20 What's the difference between HEAD, working tree and index, in Git?
2017-09-20 Data Member Order
点击右上角即可分享
微信分享提示