String str=”hello java helloworld hello java hello java hahahaha”; 求出一个字符串中java出现次数

public static int getCount(String string,String terget){
		
		int begin=0;
		int count=0;

		while((begin=string.indexOf(terget,begin))!=-1){
			
			
			count++;
			
			begin+=terget.length();
			
		}

return count;
}

	public static void main(String[] args) {
	String string="hello java helloworld hello java hello java hahahaha";
	String terget="java";
	int s=Test.getCount(string,terget);
	System.out.println(s);
		
	}
}

 

你可能感兴趣的:(Java题目)