c# – 在另一个数组中找到一个数组(byte [])?

前端之家收集整理的这篇文章主要介绍了c# – 在另一个数组中找到一个数组(byte [])?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在另一个byte []中找到byte []的最简单方法是什么?我有一种感觉,我可以用 linq做,但我不知道如何.

注意:我用[c#]搜索并没有找到任何东西,我感到很惊讶.

解决方法

这是一个简单(天真?)的方法
  1. static int search(byte[] haystack,byte[] needle)
  2. {
  3. for (int i = 0; i <= haystack.Length - needle.Length; i++)
  4. {
  5. if (match(haystack,needle,i))
  6. {
  7. return i;
  8. }
  9. }
  10. return -1;
  11. }
  12.  
  13. static bool match(byte[] haystack,byte[] needle,int start)
  14. {
  15. if (needle.Length + start > haystack.Length)
  16. {
  17. return false;
  18. }
  19. else
  20. {
  21. for (int i = 0; i < needle.Length; i++)
  22. {
  23. if (needle[i] != haystack[i + start])
  24. {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. }

猜你在找的C#相关文章