byte array Starts With - CSharp System

CSharp examples for System:Byte Array

Description

byte array Starts With

Demo Code


using System.Text;
using System;/*from  w  w w  . j  a  v a2s  . com*/

public class Main{
        public static bool StartsWith(this byte[] array1, params byte[] array2)
        {
            return array1.Compare(true, array2);
        }
        static bool Compare(this byte[] array1, bool startsWith, int[] anyMaskIndexes, params byte[] array2)
        {
            int len2 = array2.Length;
            if ((!startsWith && len2 != array1.Length) || len2 > array1.Length)
            {
                return false;
            }
            if (len2 == 0)
            {
                return true;
            }
            for (int i = 0; i < len2; i++)
            {
                if (anyMaskIndexes != null)
                {
                    bool any = false;
                    foreach (var j in anyMaskIndexes)
                    {
                        if (j == i)
                        {
                            any = true;
                            break;
                        }
                    }
                    if (any)
                    {
                        continue;
                    }
                }
                if (array1[i] != array2[i])
                {
                    return false;
                }
            }
            return true;
        }
        static bool Compare(this byte[] array1, bool startsWith, params byte[] array2)
        {
            return Compare(array1, startsWith, null, array2);
        }
        public static bool Compare(this byte[] array1, int[] anyMaskIndexes, params byte[] array2)
        {
            return array1.Compare(false, anyMaskIndexes, array2);
        }
        public static bool Compare(this byte[] array1, params byte[] array2)
        {
            return array1.Compare(false, array2);
        }
}

Related Tutorials