/** * @author PLA * 在字符串中找出连续最长的数字串 */ public static void main(String[] args) { String s = "fd76687dfsad69798796dgdg7902344342"; getNum(s); } public static void getNum(String s){ int tempCount,tempStart = 0,maxCount = 0,maxStart = 0; char[] ch = s.toCharArray(); for(int i=0;i<s.length();i++){ tempCount = 0; if(isNum(ch[i])){ tempStart = i; tempCount++; while((i<s.length()-1)&&isNum(ch[++i])){ tempCount++; } } if(tempCount>maxCount){ maxCount = tempCount; maxStart = tempStart; } } System.out.println("最大连续数字长度:"+maxCount); System.out.println(s.substring(maxStart, maxStart+maxCount)); } public static boolean isNum(char ch){ if((ch>='0')&&(ch<='9')){ return true; } return false; }