题目描述在字符串中找出连续最长的数字串,并把这个串的长度返回;如果长度相同,返回最后一个连续字符串样例输入
abcd12345ed125ss123456789
abcd12345ss54321样例输出
输出123456789,函数返回值9
输出54321,函数返回值5 函数原型:
int Continumax(String intputStr, StringBuffer outputStr)输入参数:
String intputStr 输入字符串输出参数:
StringBuffer outputStr 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串 返回值:
int 连续最长的数字串的长度
abcd12345ed125ss123456789
abcd12345ss54321样例输出
输出123456789,函数返回值9
输出54321,函数返回值5 函数原型:
int Continumax(String intputStr, StringBuffer outputStr)输入参数:
String intputStr 输入字符串输出参数:
StringBuffer outputStr 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串 返回值:
int 连续最长的数字串的长度
import java.util.regex.Pattern;public class Hello {
public static void main(String[] args) {
String str = "abcd12345ed125ss123456789";
String result = null; Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(str); while (matcher.find()) {
String matched = matcher.group(0); if ((result == null) || (matched.length() >= result.length())) {
result = matched;
}
} System.out.println(result);
}
}
import java.io.IOException;
import java.io.InputStreamReader;public class RegexTest1 { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=null;
StringBuffer sb=new StringBuffer();
while((str=br.readLine())!=null){
System.out.println(str+":"+continueMax(str,sb));
}
br.close(); } private static int continueMax(String str, StringBuffer sb) {
String[] strs=str.split("[^0-9]");
int max=0;
int index=0;
for(int i=0;i<strs.length;i++){
if(strs[i].length()>=max){
max=strs[i].length();
}
}
sb.delete(0, sb.length());
if(max==0){
sb.append("");
}
sb.append(strs[index]);
return max;
}}