方法一:
public class StatisticalStringNumber1 { public static void main(String args[]){ String string="javajava_eclipse_class_jajavavajavajdjdj"; String str="java"; int i=string.length()-string.replace(str, "").length(); System.out.println(i/str.length()); } }
方法二:
public class StatisticalStringNumber2 { public static void main(String args[]){ String string="javajava_eclipse_class_jajavavajavajdjdj"; String str="java"; String[] array=string.split(str); if(array!=null){ System.out.println(array.length-1); } } }
方法三:
public class StatisticalStringNumber3 { public static void main(String args[]){ String string="javajava_eclipse_class_jajavavajavajdjdj"; String str="java"; int counter=0; for(int i=0;i<=string.length()-str.length();i++){ if(string.substring(i,i+str.length()).equals(str)){ counter++; //substring返回一个新字符串,它是此字符串的一个子字符串。 } } System.out.println(counter); } }