字符串中找出连续最长的数字串(正则表达式)

题目描述

读入一个字符串str,输出字符串str中的连续最长的数字串

输入描述:

个测试输入包含1个测试用例,一个字符串str,长度不超过255。

输出描述:

在一行内输出str中里连续最长的数字串。
示例1

输入

abcd12345ed125ss123456789

输出

123456789
 1 /**
 2  * 
 3  * 
 4  用正则表达式 替换  非数字 字符  在用 split 分割 得到 字符串数组
 5  * @author Dell
 6  *
 7  */
 8 import java.util.Scanner;
 9 public class Main {
10     public static void main(String[] args) {
11 Scanner sc = new Scanner(System.in);
12 String str = sc.nextLine();
13 //用正则表达式 替换  非数字 字符  在用 split 分割 得到 字符串数组
14 String[] ss = str.replaceAll("[^0-9]",",").split(",");
15 //遍历 找出最大的
16 int maxSize = 0;
17 String res = "";
18 for (int i = 0; i < ss.length; i++) {
19     if (ss[i].length()>maxSize) {
20         maxSize = ss[i].length();
21         res = ss[i];
22     }
23 }
24     System.out.println(res);
25 }
26 }


猜你喜欢

转载自www.cnblogs.com/the-wang/p/8981298.html