求一方法
有一數組,內存放Y,N共32個,我想寫一方法算出數組中Y的個數,謝謝
[解决办法]
循环,判断++
[解决办法]
string[] str = new string[32] { "Y ", "N ", "N ", "Y ", "N ", "N ", "N ", "Y ", "N ", "N ", "N ", "Y ", "N ", "N ", "N ", "Y ", "N ", "N ", "N ", "Y ", "N ", "N ", "N ", "Y ", "N ", "N ", "N ", "Y ", "N ", "N ", "N ", "Y "};
Array.Sort(str);
int length = str.GetLength(0) - Array.BinarySearch(str, "Y ", null);
MessageBox.Show(length.ToString()); ;
[解决办法]
1:
char[] teststr = { 'Y ', 'N ', 'Y ', 'N ' };
Response.Write(Regex.Matches(new string(teststr), @ "Y ").Count);
2:
循环
哪个效率高些呢:)