例如:java helloworld lo
结果:Total of lo = 1

解决方案 »

  1.   

    while((temp = src.indexOf(subStr) ) != -1){
    total ++;
    src = src.substring(temp+subStr.length());
    }
      

  2.   

    网上找了个
    把符合条件的打印出来
      public static String[] getStrExpression(String regex,String str){ 
            
            List<String> temp = new ArrayList<String>();
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(str);
            while(m.find()){
                temp.add(m.group());
            }
            String[] result=new String[temp.size()];
            temp.toArray(result);
            return result;
        } public static void main(String[] a){
     String regex="lo";//一个正则可以变更 eg:l*o
            String str="java helloworld ";
            String[] result=getStrExpression(regex,str);
            for(String s:result){
                System.out.println(s);
            }
     System.out.println(result.length);
    }
      

  3.   

    public static void main(String[] args) {

    String message = "java helloworld lo";

    getSubString(message);
    }
    static void getSubString(String message) {

    String [] getMessage = message.split(" ");

    int count = 0;

    for (int i=0; i<getMessage.length; i++) {

    if(getMessage[i].equals("lo")) {
    count++;
    }
    }

    System.out.println("Total of lo = " + new Integer(count));
    }
    经测试,正确