由于我们处理的日志需要过滤一些空格,因此大部分处理日志的程序中都用到了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()函数造成的这个影响,但是当不希望去掉这些特殊字符的时候,就必须要认真详细地研究一下这个可能会出现的问题了。
 
 

最新文章

  1. 【HTTP劫持和DNS劫持】实际JS对抗
  2. IOC装配Bean(XML方式)
  3. Android 对话框(Dialog)大全 建立你自己的对话框
  4. WebApi:使用方法名或者控制器名作为接口地址
  5. js实现页面跳转
  6. [Hibernate] - Annotations - Many To Many
  7. JavaScript学习小结(一)——JavaScript入门基础
  8. HDU 2126 (背包方法数) Buy the souvenirs
  9. 如何查看MySQL中每张表占用的空间大小
  10. getting start with storm 翻译 第八章 part-2
  11. UI设计|PS软件操作应用——GIF动图
  12. 关于用户与服务端密码的校验问题 !mysql php
  13. Java是如何读到hbase-site.xml 的内容的
  14. &#39;org.springframework.beans.factory.xml.XmlBeanFactory&#39; is deprecated
  15. Python os.walk() 遍历出当前目录下的文件夹和文件
  16. 什么是HOOK(钩子):消息拦截与处理
  17. python迭代器的内置函数
  18. 【HNOI2009】梦幻布丁
  19. 使用Tomcat搭建一个可以通过公网访问的服务器(转)
  20. QQ宠物怀念版H5

热门文章

  1. 缺少所需的CD/DVD驱动器设备驱动程序
  2. 如何把rtl8188EUS驱动编译生成ko模块并且下载到rk平台Android4.2.2上使用
  3. Java基础部分全套教程.
  4. 实战:向GitHub提交代码时触发Jenkins自动构建
  5. Percona Xtrabackup 安装
  6. using中StreamWriter XmlWriter 区别
  7. GLSL 内建函数
  8. 每天一个linux命令(磁盘):【转载】df 命令
  9. java并发--Callable、Future和FutureTask
  10. 【2018.06.26NOIP模拟】T2号码bachelor 【数位DP】*