1. 程式人生 > >Java獲取字串中指定字元的個數

Java獲取字串中指定字元的個數

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;
	}
}