文本框中提取类身份证号到集合中的算法

想一想,其实只要是连串数字就不管

达到14位或者17位的数字时候,把下面那个字符直接提取,从新计算开始提取的位置


第一、确定文本框的字符流比如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
           {
              //else1
              //当开始索引存在时判断现在读的这个字符为数字时
              if(Char.IsNumber(str, count) == true)
              {
                i_end=count;
                count=count+1;//进入下一论分析中
              }
              else
              {
                //else2
                //当开始索引存在时判断现在读的这个字符不为数字时;

                if(i_end-i_start+1==17)
                {
                  i_end=count;
                  listbox.item.Add(str.Substring(i_start,i_end-i_start+1);
                 
                  i_start=-1;
                  i_end==1;
                  count=count+1;
                }
                else
                {   //else3
                    if(i_end-i_start+1==14)
                    {
                     i_end=count;
                     listbox.item.Add(str.Substring(i_start,i_end-i_start+1);
                 
                     i_start=-1;
                     i_end==1;
                     count=count+1;
                    }
                    else
                    {
                     //else4
                     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 else4
                }//end else3
              }//end else2
           }//end else1
        }//end while(计数完成就跳出,未完成就继续循环)
     }//该方法结束

posted on 2008-11-27 20:22  黄玮璘  阅读(291)  评论(0编辑  收藏  举报

导航