java.lang.String.trim(), 不仅仅去掉空格

 
由于我们处理的日志需要过滤一些空格,因此大部分处理日志的程序中都用到了java.lang.String.trim()函数。直到有一次遇到一个诡异的问题,某个包含特殊字符的字符串被trim后居然也为空(虽然这种特殊字符也没有什么太大意义…)。
 
于是查看这个特殊字段,显示为^I(在Linux下可以通过cat -A命令能够查看这个特殊字符),对应键盘上的Tab键,于是便将trim()函数拉出来看了一下:
 
public String trim() {
    int len = value.length;
    int st = 0;
    char[] val = value;    /* avoid getfield opcode */

    while ((st < len) && (val[st] <= ' ')) {
        st++;
    }
    while ((st < len) && (val[len - 1] <= ' ')) {
        len--;
    }
    return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
 
 
果然,方法中表明,如果字符小于空格,就过滤掉,仔细阅读trim()函数的注释也可以得出这个结论:
 
Returns a string whose value is this string, with any leading and trailing whitespace removed.
If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u005Cu0020' (the space character), then a reference to this String object is returned.
Otherwise, if there is no character with a code greater than '\u005Cu0020' in the string, then a String object representing an empty string is returned.
Otherwise, let k be the index of the first character in the string whose code is greater than '\u005Cu0020', and let m be the index of the last character in the string whose code is greater than '\u005Cu0020'. A String object is returned, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(k, m + 1).
This method may be used to trim whitespace (as defined above) from the beginning and end of a string.
Returns:
A string whose value is this string, with any leading and trailing white space removed, or this string if it has no leading or trailing white space.
 
 
那么,究竟什么样的字符会被删除过滤掉呢,搜索出ASCII码表:
 


 
 
 
其中可以看出,空格对应32,按照trim的逻辑,32之前的字符都是可以被过滤掉的,其中包含我们比较熟悉的水平制表符(^I),回车(^M),以及Hive中默认使用的字段分隔符^A等等。
 
正常应用情况下,我们不需要考虑trim()函数造成的这个影响,但是当不希望去掉这些特殊字符的时候,就必须要认真详细地研究一下这个可能会出现的问题了。
 
 
posted @ 2015-03-11 22:22  clamaa  阅读(734)  评论(0编辑  收藏  举报