String.trim源码如下:
public String trim() {
int len = count;
int st = 0;
int off = offset; /* avoid getfield opcode */
char[] val = value; /* avoid getfield opcode */
while ((st < len) && (val[off + st] <= ' ')) {
st++;
}
while ((st < len) && (val[off + len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < count)) ? substring(st, len) : this;
}
其中半角的空格的ascill码值为32,全角的空格的ascill码值为12288
半角tab键的ascill码值为9,全角tab键的ascill码值为9
回车的ascill码值为10。
所以trim方法除了全角的空格其它的空白字符都可以去掉。