Java获取字符串中指定字符的个数

版权声明:学习分享,共同进步 https://blog.csdn.net/Andrew_jdw/article/details/83090072
public class Demo6 {
	public static void main(String[] args) {
		String s = "javaajavacjavabbjavaeee";
		String t = "java";
		System.out.println(getCount(s,t));
	}
	
	public static int getCount(String str, String tag) {
		int index = 0;
		int count = 0;	   
		 while ((index = str.indexOf(tag)) != -1 ) {
			 str = str.substring(index + tag.length()); 
			 count++;
		}
		return count;
	}
}

猜你喜欢

转载自blog.csdn.net/Andrew_jdw/article/details/83090072