文本框中提取字符串的算法

今天想做一个提取身份证批量比对程序,心血来潮写了个基本算法,尚未验证实现身份证的号码提取(身份证可以有最后一位为字符)

仅仅实现提取数字串

进行文本框中数字串联提取的算法:
第一、确定文本框的字符流比如label_stram.Text
第二、确定一个列表框可以将提取到的数字串分隔放置,比如listbox
第三、采用计数器的方式逐字识别获取数字字符串,并将逐个字符串添加到listbox的元素中
    /// <summary>
    /// 提取字符串中为数字的字符串
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public void storeNumStr(String str)
    {
        //数字字符开始索引
        int i_start=-1;
        //数字字符终结索引
        int i_end=-1;
        //计数器
        int count=0;
   
        while(count<=str.Length)
        {
           if(i_start==-1)
           {
              //当开始索引不存在时发现数字立刻记录开始索引
              if(Char.IsNumber(str, count) == true)
               {
                 i_start=count;
                 //发现以后计数也要递增
                 count=count+1;
               }
               else
               {
                 //没有发现就计数加1
                 count=count+1;
               }
           }
           else
           {
              //当开始索引存在时判断现在读的这个字符为数字时
              if(Char.IsNumber(str, count) == true)
              {
                i_end=count;
                count=count+1;//进入下一论分析中
              }
              else
              {
                //当开始索引存在时判断现在读的这个字符不为数字时;
                i_end=count-1;
                //截取数字串
                listbox.item.Add(str.Substring(i_start,i_end-i_start+1);
                //将开始索引清除;
                i_start=-1;
                i_end==1;
                //计数继续增加
                count=count+1;
              }
           }
        }//end while(计数完成就跳出,未完成就继续循环)
     }//该方法结束

posted on 2008-11-27 19:15  黄玮璘  阅读(429)  评论(0编辑  收藏  举报

导航