java 查找子串出现的次数

来源:互联网 发布:阿里云 弹性web托管 编辑:程序博客网 时间:2024/05/03 21:17

public class main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        String totalstr = new String("123sdasds123dsadas123sdfsdfsdf123rtgfg123");
        String sub = new String("123");
        
        int count = 0;
        
        System.out.println( totalstr.indexOf("ysd"));
        int i = 0;
        while( !totalstr.isEmpty())
        {
            i = totalstr.indexOf(sub);
            
            if(i == -1)
            {
                break;
            }
            i+=3;
            count++;
            totalstr = totalstr.substring(i);
        }
        System.out.println(count);
       
    }

}

0 0
原创粉丝点击