java tips

判断是否为空格:
1. Character.isWhitespace(source.charAt(1));
java.lang.Character.isWhitespace(int codePoint) 确定指定字符(Unicode代码点)是根据Java空白。一个字符是一个Java空白字符,当且仅当它满足下列条件之一,返回true,否则返回false:

It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
It is '\t', U+0009 HORIZONTAL TABULATION.
It is '\n', U+000A LINE FEED.
It is '\u000B', U+000B VERTICAL TABULATION.
It is '\f', U+000C FORM FEED.
It is '\r', U+000D CARRIAGE RETURN.
It is '\u001C', U+001C FILE SEPARATOR.
It is '\u001D', U+001D GROUP SEPARATOR.
It is '\u001E', U+001E RECORD SEPARATOR.
It is '\u001F', U+001F UNIT SEPARATOR.

2.
public static boolean isBlank(String text) {
//        Pattern pattern = Pattern.compile("[\\s\\t]");
        Pattern pattern = Pattern.compile("\\s+");
        char[] chars = text.toCharArray();
        for(int i = 0; i < chars.length; i++) {
            String s = String.valueOf(chars[i]);
            Matcher re = pattern.matcher(s);
            boolean find = re.find();
            if(!find) {
                return false;
            }
        }

详解 "\s+"

正则表达式中\s匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]

  • \f -> 匹配一个换页
  • \n -> 匹配一个换行符
  • \r -> 匹配一个回车符
  • \t -> 匹配一个制表符
  • \v -> 匹配一个垂直制表符

你可能感兴趣的:(java tips)