string str1="abc";
string str2="asdlfjowefabclasdfasd";
怎么判断1是否在2中

解决方案 »

  1.   

    str2.contains(str1)
    效率比一楼的低,呵呵
      

  2.   

    if (str2.IndexOf(str1)>=0)

      包括
    }
    else
    {
     不包括
    }
      

  3.   

    java 写的,没用到任何api函数。
    通过flag标识得到结果,在jdk1.4.2下测试通过:可以参考一下算法            String s1="";   // The original string
          String s2="";   // compare to s1,to judge whether s1 is contain 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;}  
                 }
              }
          }
        }
      

  4.   

    str1=str1+"dd";
    str3=Replace(str1,str2)比较str3和str2的长度
      

  5.   

    str2.IndexOf(str1)为-1就是没找到,其他的数字就是位置
      

  6.   

    使用indexof(string value)
    indexof:报告指定字符串在当前System.String对象中的第1个匹配项的索引
      

  7.   

    报告指定的 String 在此实例中的第一个匹配项的索引。
    public int IndexOf(string);
      

  8.   

    string 的方法 -- public int IndexOf(string sub)