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

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. TI IPNC Web网页之进阶修改

    GoDB内嵌HTML 原始的页面里面已经有一个内嵌HTML的例子了,那就是维护支持页面.下图是稍微修改后的页面...请自行脑补. 这里使用的是上一节所说的gdo containter的方法. 打开ma ...

  2. mac下解决mysql乱码问题

    问题描述:在window平台下面数据库插入.已经查找都是很正常的,但是到mac下面查找.插入就不正常了,之后感觉是mysql的问题然后网上搜索学习了下,果然是mysql的问题.解决方案:首先你要先去看 ...

  3. VS2010 快捷键 (空格显示 绿点, Tab 显示箭头)

    转自http://www.cnblogs.com/xiaoyusmile/archive/2012/06/27/2566049.html VS2010 有用的快捷键 : Ctrl + r, ctrl ...

  4. (转)MapReduce Design Patterns(chapter 3 (part 2))(六)

    Top Ten Pattern Description Top ten模式跟前面的有很大的不同,跟输入数据大小无关,最终得到的记录数量是确定的.而在通用filtering中,输出的规模取决于输入数据. ...

  5. Android 图片压缩各种方式

       前言:由于公司项目当中需要用到压缩这块的相应技术,之前也做过的图片压缩都不是特别的理想, 所以这次花了很多心思,仔细研究和在网上找到了很多相对应的资料.为了就是 以后再做的时候直接拿来用就可以了 ...

  6. k近邻法( k-nearnest neighbor)

    基本思想: 给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的k个实例,这k个实例的多数属于某个类,就把该输入实例分为这个类 距离度量: 特征空间中两个实例点的距离是两个实例点相似 ...

  7. 产生num个不重复的随机数组

    createDiffRandom : function (from,to,num) { // 产生num个不重复的随机数组 var arr=[],json={}; // 随机数数组 , 标记json对 ...

  8. 全面进军javascript!

    前两天经过新华书店,进去转了转,又买了两本书.这次买的是<javascript学习指南>和<HTML5经典实例>(都是图灵动物系列,我已经有三本了*^_^*),其实我是想去买& ...

  9. Mat代码操作

    #include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; ...

  10. js中confirm揭示三个按钮“是”“否”“取消”

    js中confirm提示三个按钮“是”“否”“取消” 重载DOM中confirm window.confirm = function(str) {   str=str.replace(/\'/g, & ...