java一个字符串在另一个字符串中出现的次数

时间:2015-05-26 09:16:08   收藏:0   阅读:290

假设要得到字符串B在字符串A中出现的次数:

//假设字符串A和B已声明并赋值
int count=0;
int fromIndex=0;
while(fromIndex!=-1){
    fromIndex=A.indexOf(B,fromIndex);
    if(fromIndex!=-1){
        fromIndex+=B.length();;
        count++;
    }
}
System.out.println(count);

原文:http://blog.csdn.net/listener_ri/article/details/45999729

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!