1. 首页
  2. 编程面试题
  3. Java
  4. Java基础

在字符串中找出连续最长数字串,返回这个串的长度,并打印这个最长数字串。



例如:abcd12345cd125se123456789,返回9,打印出123456789


public class TestExer1 {
	public static void main(String[] args) {
		String str = "abcd12345cd125se123456789";
		
		//去掉最前和最后的字母
		str =	str.replaceAll("^[a-zA-Z]+", "");
		
		//[a-zA-Z]:表示字母范围
		//+:一次或多次
		String[] strings = str.split("[a-zA-Z]+");
		
		String max = "";
		for (String string : strings) {
			if(string.length() > max.length()) {
				max = string;
			}
		}
		System.out.println("最长的数字串:" + max + ",它的长度为:" + max.length());
	}
}

// 输出结果
最长的数字串:123456789,它的长度为:9

发布者:admin,如若转载,请注明出处:https://ai1024.vip/39646.html

QR code
//