`
jpgtama
  • 浏览: 17182 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java String 的trim 方法,并不仅仅是去掉空格

阅读更多

如果仔细看java api里面关于String.trim()方法的说明,就会明白trim()方法不是仅仅去掉空格,它去掉的是编码小于等于\u0020的字符,也就是在ASCII码里面十六进制20以前的字符。

 

 

 

 

 

 

 

Java API 写道
trim

public String trim()

Returns a copy of the string, with leading and trailing whitespace omitted.

(返回字符串的拷贝,前后空格被忽略)

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 '\u0020' (the space character), then a reference to this String object is returned.

(如果字符串为空,或者前后都是编码大于\u0020的字符,返回原有字符串的引用)


Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

(否则,大家知道的...)


Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, 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 copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

 

 

 

再看源代码就更确定了:

 

public String trim () {

        // 这里的start, end 是新字符串开始和结束的位置
	int start = offset, last = offset + count - 1;
	int end = last;



        // 下面的两个while循环计算start 和 end 的值
	while ((start <= end) && (value [start] <= ' ')) start++;
	while ((end >= start) && (value [end] <= ' ')) end--;


        // 如果值和原有的一样,就直接返回原有的字符串
	if (start == offset && end == last) return this;


        // 用 start 和 end 返回新的字符串
	return new String (start, end - start + 1, value);


}
 

 

分享到:
评论

相关推荐

    android、Java下判断两个String是否相等 、EditText输入是否为空,限定输入数字的实现

    接着,我们使用 `trim()` 方法将前后的空格去掉。最后,我们使用 `equals()` 方法将其与空字符串进行比较,如果相等,则输入为空。 限定输入数字 限定输入数字可以在 XML 文件中设置 `android:inputType` 属性为 `...

    javascript去除字符串左右两端的空格

    下面的实现方法是用到了正则表达式,效率不错,并把这三个方法加入String对象的内置方法中去。  写成类的方法格式如下:(str.trim();) 代码如下:  [removed]  String.prototype.trim=function(){  return ...

    springmybatis

    MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个...

    freemarker语法完整版

    sequence 序列,对应java 里的list 、数组等非键值对的集合 hash 键值对的集合 namespace 对一个ftl 文件的引用, 利用这个名字可以访问到该ftl 文件的资源 B 指令 if, else, elseif 语法 Java代码 ...

    net学习笔记及其他代码应用

    这并不意味着线程就会立即运行。run()方法可以产生必须退出的标志来停止一个线程。 40.接口是否可继承接口? 抽象类是否可实现(implements)接口? 抽象类是否可继承实体类(concrete class)? 答:接口可以继承接口。...

    Java-PHP-C#

    现在让我们放弃 负号 , 因为我们在表示钱币的时候并不需要用到. 我们现在指定 模式 用来匹配小数部分: ^[0-9]+(\.[0-9]+)?$ 这暗示匹配的字符串必须最少以一个阿拉伯数字开头. 但是注意,在上面模式中 "10." 是...

    java 正则表达式

    String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");} 利用正则表达式分解和转换IP地址: 下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序: function IP2V...

    正则表达式

    JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. 在JavaScript中,正则表达式是由一个RegExp对象表示的.当然,可以使用一个RegExp()构造函数来创建RegExp...

    freemarker总结

    这里所说的空值,实际上也包括那些并不存在的变量,对于一个Java的 null值而言,我们认为这个变量是存在的,只是它的值为null,但对于FreeMarker模板而言,它无法理解null值,null值和不存在的变 量完全相同. 为了处理缺失...

    2009达内SQL学习笔记

    ORDER BY子句中使用的列将是为显示所选择的列,但是实际上并不一定要这样,用非检索的列排序数据是完全合法的。 为了按多个列排序,列名之间用逗号分开。 2、支持按相对列位置进行排序。 输入 SELECT prod_id,...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    本教程并不想让你完全了解这种语言,只是能使你尽快加入开发动态web站点的行列。我假定你有一些HTML(或者HTML编辑器)的基本知识和一些编程思想。 1.简介 PHP是能让你生成动态网页的工具之一。PHP网页文件被当作...

Global site tag (gtag.js) - Google Analytics