求字符串中最大数字

 public String GetMaxLenNumber(String inputStr)
       {
           //将字符串中的字符存放到数组中,便于处理
           char[] strArray = inputStr.toCharArray();
           //开始处理的位置
           int startPos = 0;
           //当前处理的字符长度
           int tempCount = 0;
           //数字的最长长度
           int maxLen = 0;
           //数组的总长度
           int len = strArray.length;
           int pos = 0;
           while (startPos < len)
           {
               //循环中的临时最大长度
               int tempMax = 0;
               while (tempCount + startPos < len)
               {
                   //开始处理的字符
                   char c = strArray[tempCount + startPos];
                   if ((c>='0')&&(c<='9'))
                   {
                       //如果是数字
                       tempMax++;
                       if (tempMax > maxLen)
                       {
                           maxLen = tempMax;
                           pos = startPos;
                       }                       
                   }
                   else
                   {
                       //不是数字
                       tempMax = 0;
                       startPos++;
                       break;
                   }
                   tempCount++;
               }
               if (startPos + tempCount == len)
               {
                   break;
               }
               tempCount = 0;             
           }
           String s = inputStr.substring(pos,pos+maxLen);
           return s;
       } 

 判断char是否是数字

Character.isDigit('9')

 

posted on 2014-08-21 10:17  jiandan123  阅读(515)  评论(0编辑  收藏  举报

导航