如何才能把两个不同字符串中相同的子字符串取出来?

解决方案 »

  1.   

    public   static   String   getSubString(String   s1,   String   s2)   {   
      if   (s1.length()   >   s2.length())   {   
      String   temp   =   s1;   
      s1   =   s2;   
      s2   =   temp;   
      }   
      int   n   =   s1.length();   
      int   index   =   0;   
      ok:for   (;   n   >   0;   n--)   {   
      for   (int   i   =   0;   i   <   s1.length()   -   n   +   1;   i++)   {   
      String   s   =   s1.substring(i,   i   +   n);   
      if   (s2.indexOf(s)   !=   -1)   {   
      index   =     i;   
      break   ok;   
      }   
      }   
      }   
      return   s1.substring(index,   index   +   n);   
      }
    记得给分啊 
    这个我试验成功的。
      

  2.   

    跳出外层循环ok:for   (;   n   >   0;   n--)   而不是for   (int   i   =   0;   i   <   s1.length()   -   n   +   1;   i++)
      

  3.   

    相同的部分?比如:THIS IS 和  HAAB IT
    他们相同的是什么?TH I还是 I还是H IT还是其他的什么