开发者

How can I check a range of an array contains zeros?

开发者 https://www.devze.com 2023-03-18 04:12 出处:网络
I have an integer array like {1,2,3,4,5,0,0,0,0,0,2,3,4,5,7,8,0,0,0,0,0,1} I want to check if the array\'s 5th element to 9th element is all 0\'s.

I have an integer array like {1,2,3,4,5,0,0,0,0,0,2,3,4,5,7,8,0,0,0,0,0,1}

I want to check if the array's 5th element to 9th element is all 0's.

following code i tried...

if (Enumerable.Range(vHisto[i],vH开发者_StackOverflow中文版isto[ i + 30]).Contains(0))
{
    x = i+30;
    break;
}

vHisto is a list which contain some integers to check like example.


Always Linq. Always.

var sixThroughTenAreZero = new int[] {1,2,3,4,5,0,0,0,0,0,2,3}
                                    .Skip(5)
                                    .Take(5)
                                    .All(x => x == 0);


You could convert integer array to string and then using substring.

 int[] arr = {0,1,2,3,0,1};
 string results = string.Join("",arr.Select(i => i.ToString()).ToArray());

 if(results.Substring(4,9).equals("00000"))
{

}
0

精彩评论

暂无评论...
验证码 换一张
取 消