zoukankan      html  css  js  c++  java
  • 什么?字符串为空?

    在做前端项目中经常会遇到字符串的处理操作,在处理之前需要判断字符串是否为空,字符串为空的情况有很多,今天来总结一下该如何进行判断

    1.if(!value)

    当字符串为nullundefinedNaN0false""这几个时,if(value)的结果都为false,if(!value)包含了我们常见的空值情况,如果你想要简便的方式,便可以使用它

    2.单个条件的判断

    2.1:undefined

    只能用 === 运算来测试某个值是否是未定义的

    if(a===undefined){
        alert("undefined")
    }
    

    2.2:null

    // 当a为null或者a为undefined时,a==null结果都为true
    if(a==null){
        alert("空")
    }
    
    //这个只有当a为null条件才成立
    if(a===null){
        alert("空")
    }
    

    2.3:String

    // 可以判断出 "",null,undefined,false,0
    if(a == "" || a == null || a == undefined){ 
        alert("空");
    }
    
    //可以判断出 "",null,undefined
    if(!$.trim(a)){ // "",null,undefined
        alert("空");
    }
    

    2.4:Array

    // 可以判断出  "",[]
    if(a.length == 0){ 
        alert("空");
    }
    
    // 可以判断出  "",[]
    if(!a.length){
        alert("空");
    }
    

    2.5:Object

    if($.isEmptyObject(a)){ // 普通对象使用 for...in 判断,有 key 即为 false
        alert("空");
    }
    
  • 相关阅读:
    递归和迭代
    The Rose
    读周国平作品有感
    matlab最小二乘法数据拟合函数详解
    读周国平作品有感
    three.js之创建一条直线
    three.js之创建一个几何体
    Go语言标准库之strconv
    Go语言基础之网络编程
    Go语言基础之并发
  • 原文地址:https://www.cnblogs.com/wugongzi/p/13332689.html
Copyright © 2011-2022 走看看