zoukankan      html  css  js  c++  java
  • Indexof String By Byte[]


            
    /// <summary>在字节数组里搜索字符串key第一次出现的位置
            
    /// </summary>
            
    /// <param name="b1">字节数组</param>
            
    /// <param name="key">字符串</param>
            
    /// <returns>搜索为空返回-1</returns>
            public static int BytesIndexOf(byte[] b1, string key)
            {
                
    byte[] keyByte = System.Text.Encoding.Default.GetBytes(key);

                
    for (int i = 0; i < b1.Length; i++)
                {
                    
    if (b1[i] == keyByte[0&& i + keyByte.Length < b1.Length)
                    {
                        
    bool tag = true;
                        
    for (int find = 1; find < keyByte.Length - 1; find++)
                            
    if (b1[i + find] != keyByte[find]) tag = false;
                        
    if (tag) return i;
                    }
                }
                
    return -1;
            }


            
    /// <summary>在字节数组里从指定位置开始搜索字符串key第一次出现的位置
            /// </summary>
            
    /// <param name="b1">字节数组</param>
            /// <param name="start">开始搜索位置</param>
            /// <param name="key">字符串</param>
            /// <returns>搜索为空返回-1</returns>
            public static int BytesIndexOf(byte[] b1, int start, string key)
            {
                
    byte[] keyByte = System.Text.Encoding.Default.GetBytes(key);

                
    for (int i = start; i < b1.Length; i++)
                {
                    
    if (b1[i] == keyByte[0&& i + keyByte.Length < b1.Length)
                    {
                        
    bool tag = true;
                        
    for (int find = 1; find < keyByte.Length - 1; find++)
                            
    if (b1[i + find] != keyByte[find]) tag = false;
                        
    if (tag) return i;
                    }
                }
                
    return -1;
            }

  • 相关阅读:
    rals gb2312编码
    义乌给力电子商务有限公司招聘网络推广专员
    [PHP]APACHE模块化安装PHP以及MySQL 配置@Windows(PHP5)
    wordpress将远程数据下载一份到本地20110228操作过程记录
    百万邮件群发业务
    Ruby 读写文件
    万网备案专题
    新ubuntu配置过程记录
    wordpress将远程数据下载一份到本地20110228操作过程记录
    万网备案专题
  • 原文地址:https://www.cnblogs.com/bruceleeliya/p/1631422.html
Copyright © 2011-2022 走看看