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

欢迎转载,转载请务必注明出处: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
原文地址:https://www.cnblogs.com/read-the-spring-and-autumn-annals-in-night/p/12041970.html