您的位置:首页 > 其它

第25题:返回字符串中最长数字字符串的长度

2015-06-22 15:20 288 查看
欢迎转载,转载请务必注明出处:http://blog.csdn.net/alading2009/article/details/46593595

github:https://github.com/frank-cq/MyTest

第25题:在字符串中找出连续最长的数字串,把这个串的长度返回

代码

package test025;

/**
* Created by cq on 2015/6/22.
* 第25题:在字符串中找出连续最长的数字串,把这个串的长度返回。
*/
public class Test025 {
public static int getLenOfMaxNumSubstring(String str){
if (str == null){
return 0;
}

int maxLen = 0, count = 0;
for (int i=0; i<str.length(); i++){
if (str.charAt(i) >= '0' && str.charAt(i) <= '9'){
count++;
}
else{
if (count > maxLen){
maxLen = count;
//子字符串起始位置:i-count
}
count = 0;
}
}

//若count较大,子字符串起始位置为str.length()-count
return maxLen > count ? maxLen:count;
}

public static void main(String[] args){
System.out.println(getLenOfMaxNumSubstring("123sss12345dse12345678"));
}
}


执行结果

Connected to the target VM, address: '127.0.0.1:6254', transport: 'socket'
8
Disconnected from the target VM, address: '127.0.0.1:6254', transport: 'socket'

Process finished with exit code 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: