请问我想判断字符串以什么开头这样写对不对啊
String string = "theerwiiuo";
Pattern pattern = Pattern.compile("^the");
Matcher m = pattern.matcher(string);
System.out.print(m.matches());

解决方案 »

  1.   

    String string = "theerwiiuo";
    Pattern pattern = Pattern.compile("^the.*");
    Matcher m = pattern.matcher(string);
    System.out.print(m.matches());
      

  2.   

    为什么不用String类的startsWith方法呢?
      

  3.   

    你那句就匹配一个the,不是匹配以the开头的任意字符串。
      

  4.   

    因为你用的是 Matcher#matches 方法,这个方法要全部匹配正则表达式才会返回 true你用 Matcher#lookingAt 方法就可以了