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
);
作者:Chuck Lu GitHub |
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).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:
~
(bitwise complement) operator<<
(left shift),>>
(right shift), and>>>
(unsigned right shift) operators&
(logical AND),|
(logical OR), and^
(logical exclusive OR) operatorsThose operators are defined for the
int
,uint
,long
, andulong
types. When both operands are of other integral types (sbyte
,byte
,short
,ushort
, orchar
), their values are converted to theint
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 toint
or have the result type asint
.The
&
,|
, and^
operators are also defined for operands of thebool
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: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 need0011
, or00000011
.How is it done?
回答1
11
is binary representation of3
. The binary representation of this value is2
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