isEmpty 与 isBlank
两个方法在本质上都是判断字符串是否为空,只是在处理一些空白符的时候有区别
下面是isEmpty()的源码
public boolean isEmpty() {
return value.length == 0;
//isEmpty在判断字符串的长度是否为0
}
这是isBlank的源码
public boolean isBlank() {
return indexOfNonWhitespace() == length();
}
@Stable
private final byte[] value;
private int indexOfNonWhitespace() {
return isLatin1() ? StringLatin1.indexOfNonWhitespace(value)
: StringUTF16.indexOfNonWhitespace(value);
}
boolean isLatin1() {
return COMPACT_STRINGS && coder == LATIN1;
}
@Native static final byte LATIN1 = 0;
@Native static final byte UTF16 = 1;
static final boolean COMPACT_STRINGS;
static {
COMPACT_STRINGS = true;
}
public static int indexOfNonWhitespace(byte[] value) {
int length = value.length;
int left = 0;
while (left < length) {
char ch = getChar(value, left);
if (ch != ' ' && ch != '\t' && !CharacterDataLatin1.instance.isWhitespace(ch)) {
//这里看到isBlank对空格和tab还有一些其他的符号做了额外的处理
break;
}
left++;
}
return left;
}
public int length() {
return value.length >> coder();
}
byte coder() {
return COMPACT_STRINGS ? coder : UTF16;
}