`
brandNewUser
  • 浏览: 446414 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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()函数造成的这个影响,但是当不希望去掉这些特殊字符的时候,就必须要认真详细地研究一下这个可能会出现的问题了。
 
 
  • 大小: 110.5 KB
分享到:
评论

相关推荐

    Js里面给String添加trim()方法,实现去掉字符串两边空格

    String.trim()Js里面给String添加trim()方法,实现去掉字符串两边空格String.trim()Js里面给String添加trim()方法,实现去掉字符串两边空格String.trim()Js里面给String添加trim()方法,实现去掉字符串两边...

    String.prototype.trim:适用于String.prototype.trim的ES5规范的垫片

    如果不可用,请调用其“ shim”方法对String.prototype.trim进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作,并符合规范( 和 )。 最常见的用法: var assert = require ( 'assert' ) ; var ...

    技巧:MyBatis 中的trim标签,好用!.zip

    技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip技巧:MyBatis 中的trim标签,好用!.zip技巧:...

    js 自定义trim去除字符串左右杂质

    JAVA中String 有trim()方法去除字符串左右的空格,js中自定义trim方法,去除字符串左右的杂质,可以去除逗号、句号、空格等等特殊字符。

    vb去除前后空格trim()函数使用

    vb去除前后空格trim()函数使用

    StringAPI.java

    Java String 类型 API 测试代码 1.String和char[]之间的转换 toCharArray(); 2.String和byte[]之间的转换 getBytes() Arrays工具类 : Arrays.toString(names) String类 String replace(char oldChar, ...

    js String.prototype.trim字符去前后空格的扩展

    最近学习js的时候发现的这个函数,这样很方便地去除前后空格,用正则实现,简单方便。下面软件开发网小编就为大家分享一下几种实现方式。 String.Prototype.trim() trim()返回一个字符串两端空白字符被删除的新字符...

    Java string.trim()究竟去掉了什么

    主要介绍了Java string.trim()究竟去掉了什么,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    Java 免费给手机发飞信

    二 Exception in thread "main" java.lang.RuntimeException: Your license has expired. Please registe ...,Your key is @2AC*1A97~C@6DCF6B8FEFE03@6B4C&CCAFF92 需要key的 可以去这弄...

    txt文档转化为String

    将.txt文档转化为String类型 具体代码如下: package IndexSearch; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * 将txt的路径,将txt文档转换为String内容 ...

    java解析Properties配置文件为对象Bean

    if(typeName.equals("java.lang.String")) { field.set(obj, value); }else if(typeName.equals("int")) { field.setInt(obj, Integer.parseInt(value)); }else if(typeName.equals("float"))...

    java 技巧 与 实例

    这个String包,然后才可以用b.trim() 去掉左右的空格函数,这里b是声明的String文件. dispose(); //释放资源 String.valueOf(a) //这个是将整型的a转化成文本 Integer.parseInt(args[0])这个将文本型转化成整型

    企业固定资产管理系统

    String uid=du.numbertex.getText().trim(); String sql="select * from users where uid="; if(uid.equals("")) { JOptionPane.showMessageDialog(null,"请输入用户编号"); return; } if(btn==du...

    java中string.trim()函数的作用实例及

    主要介绍了java中string.trim()函数的作用实例及源码,具有一定借鉴价值,需要的朋友可以参考下

    基于JAVA语言的计算器图形界面设计.doc

    //该类为主类创建数字按钮对象 class NumMouseListener implements MouseListener{ public void mouseClicked(MouseEvent e) { String num = ((Button)e.getSource()).getLabel().trim(); if(ary[1] == null) { ary...

    Delphi Trim删除字符串首尾空格的实例.rar

    Delphi 删除字符串首尾空格的实例,过滤字符串,替换字符串,删除指定部位的字符串,具体到本例中,是删除字符串开头和结尾处的空格,本例子其实简单,使用内置的Trim函数即可实现,可当作是Delphi Trim函数的用法...

    java写的将字符串中的中文转化为拼音等一系列方法

    * 文件名:PingYinUtil.java 拼音工具类 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com * */ public class PingYinUtil { /** * 将字符串中...

    heading_control.rar_desired _linearization _trim_uav_uav control

    This script will setup the nonlinear simulation (UAV_NL.mdl) and call trim and linearization routines. Select the desired aircraft here in this script, via the "UAV_config()" function call.

    批量删除空格.bat

    批量删除文档中空格并生成当前文件夹中文件名 说明:1.把BAT复制到需要删除及提取文件名的文件夹;2.双击运行;3.运行后该文件夹下包含空格的文件名会被删除包含的空格符;4.生成的EXECL中就是当前文件夹下的所有一...

    Javaswing登录页面的实现

    String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录...

Global site tag (gtag.js) - Google Analytics