String str="asd   asd";
Pattern patt1=Pattern.compile("^\\s$");
Matcher matcher=patt1.matcher(str);
boolean flag=matcher.find();
System.out.println(flag);
我的意思是验证str中的空格 可是为什么打印出的结果是false?
请教解法,验证字符串中是否有空格 
PS:字符串可以是数字和字母

解决方案 »

  1.   

    public class Test {


    //测试使用
    public static void main(String[] args) {
    String str="asd   asd";
    Pattern patt1=Pattern.compile("\\s+");
    Matcher matcher=patt1.matcher(str);
    boolean flag=matcher.find();
    System.out.println(flag);

    }}
      

  2.   

    Pattern patt1=Pattern.compile("^.+\\s.+$");那还不如不用:Pattern patt1=Pattern.compile("^.+\\s.+$");
      

  3.   

    开始是指的首字母为" ",如" adsfaet"
    而结束是指尾字母为" ",如"adsfaet  "
    你的要求是字符串里包含" " 而不需要指定哪个位置是" ".