美文网首页
关于string.trim()

关于string.trim()

作者: 单小单单 | 来源:发表于2020-04-10 19:36 被阅读0次

工作中经常可以看到下面的代码片段:

if(str==null||str.trim().length()==0){......}

这段代码的作用是判断一个字符串是否为空值。代码虽简单,但这种方式有个问题:使用了trim()方法。

trim()方法本身没什么不好,只是被用错了地方。

我们看看trim()是怎么工作的。

public String trim() {intlen= count;intst =0;intoff = offset;/* avoid getfield opcode */char[] val = value;/* avoid getfield opcode */while ((st 0) || (len< count)) ? substring(st,len) : this;    }

publicStringsubstring(intbeginIndex,intendIndex){if(beginIndex <0) {thrownewStringIndexOutOfBoundsException(beginIndex);}if(endIndex > count) {thrownewStringIndexOutOfBoundsException(endIndex);}if(beginIndex > endIndex) {thrownewStringIndexOutOfBoundsException(endIndex - beginIndex);}return((beginIndex ==0) && (endIndex == count)) ?this:newString(offset + beginIndex, endIndex - beginIndex,value);    }

可以看到, 当字符串前后有空白字符时,trim()会生成一个新的String对象。

我们可以直接使用apache commons lang包中的StringUtils类的isEmpty()或isBlank()方法来判断字符串是否为空。

如果你不想引入第三方的jar包,想要自己实现,那么也可以参考apache的实现方式:

*

Checksifa CharSequenceisempty ("") ornull.

    *    *
     * StringUtils.isEmpty(null)      =true* StringUtils.isEmpty("")        =true* StringUtils.isEmpty(" ")       =false* StringUtils.isEmpty("bob")     =false* StringUtils.isEmpty("  bob  ") =false* 
    *    *

NOTE: This method changedinLang version2.0.    * It no longer trims the CharSequence.    * That functionalityisavailableinisBlank().

    *    *@paramcs  the CharSequence to check, may benull*@return{@codetrue}ifthe CharSequenceisempty ornull*@since3.0Changed signature from isEmpty(String) to isEmpty(CharSequence)    */publicstatic boolean isEmpty(CharSequence cs) {returncs ==null|| cs.length() ==0;    }/**    *

Checks if a CharSequence is whitespace, empty ("") or null.

    *    *
     * StringUtils.isBlank(null)      = true     * StringUtils.isBlank("")        = true     * StringUtils.isBlank(" ")       = true     * StringUtils.isBlank("bob")     = false     * StringUtils.isBlank("  bob  ") = false     * 
    *    *@paramcs  the CharSequence to check, may be null    *@return{@codetrue} if the CharSequence is null, empty or whitespace    *@since2.0    *@since3.0 Changed signature from isBlank(String) to isBlank(CharSequence)    */publicstatic boolean isBlank(CharSequence cs) {        int strLen;if(cs ==null|| (strLen = cs.length()) ==0) {returntrue;        }for(int i =0; i < strLen; i++) {if(Character.isWhitespace(cs.charAt(i)) ==false) {returnfalse;            }        }returntrue;    }

文章转自:https://my.oschina.net/parthenon/blog/151489

相关文章

  • 关于string.trim()

    工作中经常可以看到下面的代码片段: if(str==null||str.trim().length()==0){....

  • string.trim()

    从字符串中移除前导空格、尾随空格和行终止符。 https://msdn.microsoft.com/library...

  • 原生 知识点(个人记忆)

    Object.assign()=> MDN String.trim()=> MDN 获取select下拉选择框的o...

  • NULL和“ ”的区分

    null和""的区别 问题一: null和""的区别 String s=null; string.trim()就会...

  • java基础,实用函数

    String.trim()//去除多余的空格,或可以用来判断字符串是否全为空格

  • js 去除字符串前后的空格

    ES5中已经有了trim()方法....直接使用:string.trim()会有浏览器版本限制:JavaScrip...

  • 09-面向对象作业第4部分

    71、String.trim()方法的作用? 72、如何去除一个字符串当中所有的空格? 73、什么是不可变字符串?...

  • C#删除字符串中的空格

    你或许知道你能使用String.Trim方法去除字符串的头和尾的空格,不幸运的是. 这个Trim方法不能去除字符串...

  • 关于关于关于

    他们爱他们自己,不爱你 他们爱你是他们的母亲妻子女儿姐妹 他们不爱你 直到你死的时候,爱才产生,与遗忘同时 那也不...

  • 光明人生

    关于出生 关于成长 关于求学 关于青春期 关于恋爱 关于择业 关于婚姻 关于养生 关于家庭 关于人际 关于教子 关...

网友评论

      本文标题:关于string.trim()

      本文链接:https://www.haomeiwen.com/subject/zjudmhtx.html