编写一个方法来查找在另一个字符串里的特定字符串。如果字符串存在,则方法必须返回真。例如:isSubString(“cat”, “The cat in the hat.”)是true,而isSubString( “bat”, “The cat in the hat.”)是false。
另外,验证边界条件也要遇到。
 isSubString(“The”, “The cat in the hat,”)是true
 isSubString(“hat.”, “The cat in the hat.”)是true。
提示-可以使用String类的charAt(int index)方法找到某个字符串中的特定字符;index是从0开始。例如:“cat”.charAt(0)是‘c’、 “cat”.charAt(1)是 ‘a’、 “cat”.charAt(2)是 ‘t’。length方法返回字符串中的字符数目:例如:“cat”.length()是3。请那位大哥帮忙解决一下!

解决方案 »

  1.   

    if("The cat in the hat".indexOf("cat")!=-1){
       //is substring
    }
      

  2.   

    contains
    public boolean contains(CharSequence s)
    Returns true if and only if this string contains the specified sequence of char values. Parameters:
    s - the sequence to search for 
    Returns:
    true if this string contains s, false otherwise 
    Throws: 
    NullPointerException - if s is null
    Since: 
    1.5 
    public class WORK{
    public static void main(String a[]){
    //StringBuffer sbuf = new StringBuffer();
    String s="The cat in the hat";
    String s1 = "The";
    System.out.println(s.contains(s1));
        }
    }