比如"ok"字符串在"nihaoksdoksad"中出现两次

解决方案 »

  1.   

    String str1 = "nihaoksdoksad";
    String str2 = "ok";
    int total = 0;
    for (int i = 0; i < str1.length(); i ++){
      if(str1.indexOf(str2) == 0){
        total ++;
      }
    }
    System.out.println(str1+"中含有"+total+"个"+str2);
      

  2.   

    上面写错了:
    String str1 = "nihaoksdoksad";
    String str2 = "ok";
    int total = 0;
    for (String tmp = str1; tmp != null&&tmp.length()>=str2.length();){
      if(tmp.indexOf(str2) == 0){
        total ++;
      }
      tmp = tmp.substring(1);
    }
    System.out.println(str1+"中含有"+total+"个"+str2);
    上面我写的算法是字符可以覆盖的查找:
    下面给你一种不允许字符覆盖的查找:
    String str1 = "nihaokokosdokosad";
    String str2 = "oko";
    int total = 0;
    for (String tmp = str1; tmp != null&&tmp.length()>=str2.length();){
      if(tmp.indexOf(str2) == 0){
        total ++;
        tmp = tmp.substring(str2.length());
      }else{
        tmp = tmp.substring(1);
      }
    }
    System.out.println(str1+"中含有"+total+"个"+str2);对于
    String str1 = "nihaokokosdokosad";
    String str2 = "oko";
    两种查找结果是有区别的,不知道lz要哪种?
      

  3.   

    String str = "abcabcab";String[] newStr = str.replaceall(str,"abc");System.out.println(newStr.length-1);
      

  4.   

    ls狠~把str换成"abc",但怎么能返回string[],你是不是想用str.split("abc")~
    但明显这个方法不行,你可以测一下str="abcabcabc",看看你得到的长度将驶多少~~
      

  5.   

    还是得用indexOf和循环.
    顺便同意楼上.
      

  6.   

    String str1 = "nihaoksdoksad";
    char []c=str1.toCharArray();
    int total=0;
    for(int i=0;i<c.length-1;i++)
    if(c[i]=='o'&&c[i+1]=='k')
    total++;
      System.out.println(str1+"中含有"+total+"个ok");
      

  7.   

    ~~~~`
    你写程序应该有供用性,你能只就事论事,
    如果把"ok"改成"ook"或别的什么,那你的程序步就要重新写了~你写的时候就用该考虑,这段程序或这个函数怎么能够适用所有的串,这样才可以称之为比较完整的程序~
      

  8.   

    public int countToken(String str,String token){
    int count=0;
    while(str.indexOf(token)!=-1){
    count++;
    str = str.substring(str.indexOf(token)+token.length());
    }
    System.out.println(token+"出现的次数:"+count);
    return count;
    }
      

  9.   

    String str1 = "nihaoksdoksad";
    char []c=str1.toCharArray();
    int total=0;
    for(int i=0;i<c.length-2;i++)
    if(c[i]=='o'&&c[i+1]=='o'&&c[i+2]='k')
    total++;
    System.out.println(str1+"中含有"+total+"个ok");//你写的时候就用该考虑,这段程序或这个函数怎么能够适用所有的串,这样才可以称之为比较完整的程序~
    我觉得你这句话明显就有问题了 ,如果一个程序能解决所有的问题,那还要程序员干什么
      

  10.   

    String input = "nihaoksdoksad";
    String target = "ok";
    int count = 0;
    while (Pattern.compile(Pattern.quote(target)).matcher(
    input).find()) {
    count++;
    }count就是你想要的!
      

  11.   

    /**
    *计算一个字符串在另一个字符串中出现的次数
    *@author : Eastsun
    */
    public class Count{
        public static int count(String text,String sub){
            int count =0, start =0;
            while((start=text.indexOf(sub,start))>=0){
                start += sub.length();
                count ++;
            }
            return count;
        }
        public static void main(String[] args){
            String text ="nihaoksdoksad";
            String sub ="ok";
            System.out.println(count(text,sub));
        }
    }
      

  12.   

    hacklew1985() ( ) 信誉:100    Blog   加为好友  2007-04-09 13:12:14  得分: 0  
     
     
       String str1 = "nihaoksdoksad";
    char []c=str1.toCharArray();
    int total=0;
    for(int i=0;i<c.length-2;i++)
    if(c[i]=='o'&&c[i+1]=='o'&&c[i+2]='k')
    total++;
    System.out.println(str1+"中含有"+total+"个ok");//你写的时候就用该考虑,这段程序或这个函数怎么能够适用所有的串,这样才可以称之为比较完整的程序~
    我觉得你这句话明显就有问题了 ,如果一个程序能解决所有的问题,那还要程序员干什么
      不要生气,我不是针对个人的~我觉得你应该看一看什么叫做合格的程序,什么叫合格的程序员,也就是想做一个合格的程序员的起码条件~~这些都是在写程序之前有必要知道的~~像这种程序,没人愿意去维护的~
      

  13.   

    String[] result = str.split("ok");count = result.length - 1;
      

  14.   

    "nihaoksdoksad".split("ok").length - 1
      

  15.   

    "abcabcabc".replace("abc", "abc*").split("abc").length - 1
      

  16.   

    ("abcabcabc"+"*").split("abc").length - 1
      

  17.   

    private static int search(String strTarget, String strSearch) {
    int result = 0;
    String strCheck = new String(strTarget);
    for (int i = 0; i < strTarget.length();) {
    System.out.println(i);
    int loc = strCheck.indexOf(strSearch);
    if (loc == -1) {
    break;
    } else {
    result++;
    i = loc + strSearch.length();
    strCheck = strCheck.substring(i);
    }
    }
    return result;
    }
      

  18.   

    public class replaceStr{
    public static void main(String[] args){
    String str = "aaabbbcccdddaaabbccddaabbcccdd";
    String subStr = "aa";
    String replaceString = "ff";
    int total = 0;
    StringBuffer finalStr = new StringBuffer();
    int begin = 0;
    int end = 0;
    while((end = str.indexOf(subStr,begin)) >= 0){
    finalStr.append(str.substring(begin,end));
    finalStr.append(replaceString);
    begin = end + subStr.length();
    total++;
    }
    finalStr.append(str.substring(begin));
    System.out.println(finalStr);
    System.out.println("total =" + total);
    }
    }
      

  19.   

    String str = "abaabbabcabd";
    System.out.println(str.length() - str.replaceAll("ab", "a").length());
      

  20.   

    replace后
    长度相减
    再除长度
      

  21.   

    先这样写吧,好象还有另一种方法的.
    这样应该会快点。^-^import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class TE {
     public static void main(String[]args){
      TE t=new TE();
      String ok="ok";
      String ok2="nihao0ksdoksadokok"; 
      System.out.println(" the num is "+t.matchNum(ok, ok2));
      ok="ao";
      System.out.println(" the num is "+t.matchNum(ok, ok2));
     }
     
     
     /**
      * 
      * @param first 需要匹配的ok
      * @param second 被匹配的String
      * @return 匹配次数
      */
     public int matchNum(String ok,String second){
      
      String pattern="("+ok+"){1}";
      System.out.println(" the pattern is "+pattern);
      Pattern p=Pattern.compile(pattern);
      Matcher m=p.matcher(second);
      int i=0;
      while(m.find()){
       i++;
      }
      return i;
     }
    }
      

  22.   

    刚学java那会写的,从屏幕输入两个字符串str1,str2. 判断str1是否包含 str2 希望能对你有所启发。import java.util.*;
    import javax.swing.*;public class Is_Contain {
       public static void main(String[] args)
       {
           
      String s1;   // The original string
          String s2;   // compare to s1,to judge whether s1 is contain s2
          
          s1=JOptionPane.showInputDialog("Enter M String:");
          s2=JOptionPane.showInputDialog("Enter child String:");
          System.out.println("s1:"+s1+"\n"+"s2:"+s2);
          char d[];
          char e[];
             d=s1.toCharArray();
          e=s2.toCharArray();
          boolean flag=true;
              if(s1.length()<s2.length())  //if length of s2 is longer than s1. Return false 
           {
              flag=false;
           }
         else
         {
          for(int i=0;i<s1.length();i++)
          {
             if(e[0]==d[i])
              {
                for(int j=0;j<s2.length();j++)
                 {
                    if(flag==false) 
                      {  flag=true;
                      }                if(e[j]!=d[i+j]) 
                       { flag=false;break;}  
                 }
              }
          }
        }
    }
    }
      

  23.   

    【同意】  superdullwolf(超级大笨狼,每天要自强,MVP) ( ) 信誉:99    Blog   加为好友  2007-4-9 20:06:20  得分: 0  
    replace后
    长度相减
    再除长度
    【顶】public int count(String one, String two) {
    int oneLen = one.length();
    int twoLen = two.length();
    twoLen = twoLen - two.replace(one, "").length();
    return twoLen / oneLen;
    }
      

  24.   

    using System.Text.RegularExpressions  private int ReturnCount(string sour, string pattern)
            {
                Int32 count = 0;
                Regex reg = new Regex(pattern);
                foreach (Match mc in reg.Matches(sour))
                {
                    count++;
                }
                return count;
            }
      

  25.   

    改掉
    public int count(String one, String two) {
         int oneLen = one.length();
         int twoLen = two.length();
         twoLen = twoLen - two.replace(one, "").length();
         return oneLen == 0 ? 0 : (twoLen / oneLen);
    }
      

  26.   

    同意ls的
    ("abcabcabc"+"*").split("abc").length - 1
      

  27.   

    【同意】  superdullwolf(超级大笨狼,每天要自强,MVP) ( ) 信誉:99    Blog   加为好友  2007-4-9 20:06:20  得分: 0  
    replace后
    长度相减
    再除长度
    【顶】public int count(String one, String two) {
    int oneLen = one.length();
    int twoLen = two.length();
    twoLen = twoLen - two.replace(one, "").length();
    return twoLen / oneLen;
    }不知道效率怎么样,不过这种方法很有创意,顶一个
      

  28.   

    int nQCount = StringUtils.countMatches(sPrepareSql, "?"); //占位符个数
    阿门
      

  29.   

    int count = new Regex("ok").Matches("ok.hhh,http://www.5lequ.com ok").Count
      

  30.   

    public static int getAppearTimes(String target, String appear) {
    return (" " + target.toUpperCase() + " ").split(appear.toUpperCase()).length - 1;
    }