现在的位置: 首页 > 综合 > 正文

在字符串中找出连续最长的数字串

2017年10月28日 ⁄ 综合 ⁄ 共 611字 ⁄ 字号 评论关闭
/**
	 * @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;
	}

抱歉!评论已关闭.