首页 > 编程语言 > 详细

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

时间:2017-05-06 19:36:59      阅读:369      评论:0      收藏:0      [点我收藏+]

trim  /[tr?m] / 英文意思:整理,修理,修剪,整齐的

trim()的作用:去掉字符串首尾的空格。

 

public static void main(String arg[]){  
  
        String a=" hello world ";  
  
        String b="hello world";  
  
        System.out.println(b.equals(a));  
  
        a=a.trim();//去掉字符串首尾的空格  
  
        System.out.println(a.equals(b));  
    }

 

执行结果:
a: hello world  ,false
a:hello world,true

 

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;
    }

 

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

原文:http://www.cnblogs.com/111testing/p/6817798.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!