请问java中查找一个字符串是否在另外一个字符串的函数是什么呢?

解决方案 »

  1.   

    用String.indexof(String )或lastIndexOf()它们原来的用途是:
    indexOf()和lastIndexOf()
    indexOf() 查找字符或者子串第一次出现的地方。
    lastIndexOf() 查找字符或者子串是后一次出现的地方。引申以后 ,可以满足你的要求程序如下
    import java.io.*;
    import java.util.*;
    public class FindSubString 
    {
            public void GotoFind(String str1 , String str2)
    {

    int strleng1 , strleng2 ;
    strleng1 = str1.length();
    strleng2 = str2.length();
    if(strleng1 > strleng2)
    {
    int result = str1.indexOf(str2) ;
    if(result ==-1)
    {
    System.out.println("string2 is not the substring of String1 ");
    }
    else
    {
    System.out.println("string2 is substring of String1 ");
    }


    }
    else
    {
    int result = str2.indexOf(str1) ;
    if(result ==-1)
    {
    System.out.println("string1 is not the substring of String2 ");
    }
    else
    {
    System.out.println("string1 is substring of String2 ");
    }

    }


    }
    public static void main(String args[])
    {
    String str1 = "babaacaabaferaaba" ; 
    String str2 = "aaa" ;
    FindSubString fss = new FindSubString();
    System.out.println("the first is "+str1.indexOf(str2));
    if(str1 == str2)
    {
    System.out.println("OOOOO");
    }
    else
    {
    fss.GotoFind(str1 ,str2);
    }
    }
    }
      

  2.   

    str1.contains(str2);内部使用了str1.indexOf(str2)!=-1实现的!
      

  3.   

    public boolean contains(CharSequence s)当且仅当此字符串包含 char 值的指定序列时,才返回 true。 参数:
    s - 要搜索的序列 
    返回:
    如果此字符串包含 s,则返回 true,否则返回 false 
    抛出: 
    NullPointerException - 如果 s 为 null
      

  4.   

    用String.indexof(String )或lastIndexOf()