编写一个方法来查找在另一个字符串里的特定字符串。如果字符串存在,则方法必须返回真。例如: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。我初学JAVA请那位大哥帮我解决一下!本人在此先说声谢谢了。
我编的太麻烦了!

解决方案 »

  1.   

    import java.util.regex.*;class Regex1{
           public static void main(String args[]) {
                  System.out.println("是否存在?"+isSubstring(args[0],args[1]));
           }
           public static boolean isSubstring(String s1,String s2){
                  Pattern p = Pattern.compile(s1);
                  Matcher m = p.matcher(s2);   
                  return m.find();
           }
    }