由于我们处理的日志需要过滤一些空格,因此大部分处理日志的程序中都用到了java.lang.String.trim()函数。直到有一次遇到一个诡异的问题,某个包含特殊字符的字符串被trim后居然也为空(虽然这种特殊字符也没有什么太大意义…)。
 
于是查看这个特殊字段,显示为^I(在Linux下可以通过cat -A命令能够查看这个特殊字符),对应键盘上的Tab键,于是便将trim()函数拉出来看了一下:
 
  1. public String trim() {
  2. int len = value.length;
  3. int st = 0;
  4. char[] val = value; /* avoid getfield opcode */
  5.  
  6. while ((st < len) && (val[st] <= ' ')) {
  7. st++;
  8. }
  9. while ((st < len) && (val[len - 1] <= ' ')) {
  10. len--;
  11. }
  12. return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
  13. }
 
果然,方法中表明,如果字符小于空格,就过滤掉,仔细阅读trim()函数的注释也可以得出这个结论:
 
  1. Returns a string whose value is this string, with any leading and trailing whitespace removed.
  2. 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.
  3. 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.
  4. 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).
  5. This method may be used to trim whitespace (as defined above) from the beginning and end of a string.
  6. Returns:
  7. 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()函数造成的这个影响,但是当不希望去掉这些特殊字符的时候,就必须要认真详细地研究一下这个可能会出现的问题了。
 
 

java.lang.String.trim(), 不仅仅去掉空格的更多相关文章

  1. Java笔记之java.lang.String#trim

    String的trim()方法是使用频率频率很高的一个方法,直到不久前我不确定trim去除两端的空白符时对换行符是怎么处理的点进去看了下源码的实现,才发现String#trim的实现跟我想像的完全不一 ...

  2. 117、Java中String类之去掉左右空格

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. java.lang.String

    1.String 是一个类,广泛应用于 Java 程序中,相当于一系列的字符串.在 Java 语言中 strings are objects.创建一个 strings 最直接的方式是 String g ...

  4. java:常用类(包装类,equals和==的比较,Date,java.lang.String中常用方法,枚举enum)

    *包装类: 将基本类型封装成类,其中包含属性和方法以方便对象操作. *byte---->Byte *short--->Short *long--->Long *float---> ...

  5. javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String

    javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String javax.el.Bean ...

  6. groovy --不注意的小错误(java.lang.String.positive() is applicable)

    sql 语句拼接报错: No signature of method: java.lang.String.positive() is applicable for argument types: () ...

  7. java中string.trim()函数的使用

    java中string.trim()函数的的作用是去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String ...

  8. java.lang.String 类源码解读

    String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public fin ...

  9. java.lang.String & java.lang.StringBuilder

    java.lang.String & java.lang.StringBuilder String 成员方法 作用 public charAr(int index) 返回给定位置的代码单元 p ...

随机推荐

  1. qml 知识积累

    本文仅仅只是记录,怕日后遗忘. ListView相关应用 https://www.cnblogs.com/SaveDictator/p/8192391.html ListView 清空操作 https ...

  2. Draggable拖动

    Draggable(拖动)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 EasyUI中Draggable(拖动)组件的使用方法,这个组件不依赖于其他组件. 1.加载方式 / ...

  3. [知识图谱] 环境配置:Java8 + Maven3 + HBase + Titan

    1.Java Java8安装配置 2.Maven Linux下的Maven安装与配置 3.Hbase 官方安装教程:http://s3.thinkaurelius.com/docs/titan/1.0 ...

  4. 创建你的第一个Flutter应用程序

    前言 Flutter,Google推出的跨平台开发框架.就在前几天,Flutter的首个发布预览版(Release Preview 1)正式发布! 即将迎来Flutter 正式版(1.0).本篇将带你 ...

  5. linux中使用yum进行软件的安装

    yum 仓库 配置信息/etc/yum.reposd/ [linuxcast]name="this is soft ware"baseurl="http://ww.bai ...

  6. Eclipse web工程 部署 三种方式 2

    Eclipse web工程 部署 三种方式 2.插件 tomcat插件下载: http://www.eclipsetotale.com/tomcatPlugin.html 解压缩后, com.sysd ...

  7. BZOJ:5092 [Lydsy1711月赛]分割序列(贪心&高维前缀和)

    Description 对于一个长度为n的非负整数序列b_1,b_2,...,b_n,定义这个序列的能量为:f(b)=max{i=0,1,...,n}((b_1 xor b _2 xor...xor ...

  8. LOJ2425 NOIP2015 运输计划 【二分+LCA+树上差分】*

    LOJ2425 NOIP2015 运输计划 LINK 题意:给你一颗树,可以将任意一条边的权值变成0,然后求m条路径的长度的最小值 思路: 先二分最后的距离ans,然后我们把路程大于ans的所有路径拿 ...

  9. NSURLSession学习笔记(一)简介

    一.URL Session的基本概念 1.三种工作模式: 默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁盘缓存的持久化策略,使用用户keychain ...

  10. JavaScript for 循环累加 json 字符串

    var msg = {"status":1,"data":[{"id":"12","words":& ...