js 判断字符串忽略大小写 类似于java 的equalsIgnoreCase

版权声明:第一次写文章,有什么需要补充的还望各位大神多多指教。 https://blog.csdn.net/mengxiangxingdong/article/details/81117842

//测试js 忽略大小写的方法

    <script>

        String.prototype.equalsIgnoreCase = function(anotherString) {
            if(this === anotherString){  //如果两者相同   否则判端两个的大小写是否为null
                return true;
            }
            //因为 typeof(null) = object  typeof(undefined) = undefined  实际上也是判端了这两个不为空
            if(typeof(anotherString)==='string'){ //this!=null&&this!=undefined &&anotherString!=null&& anotherString!=undefined
                return this.toLowerCase() == anotherString.toLowerCase(); //
            }
            return false;
        }
        //调用方式 
        console.log("1".equalsIgnoreCase("1")); // true
        console.log("1".equalsIgnoreCase(1)); //false
        console.log("测试AAA".equalsIgnoreCase("测试aaa")); // true
        console.log("测试AAA".equalsIgnoreCase("测试AAA")); //true
    </script>

猜你喜欢

转载自blog.csdn.net/mengxiangxingdong/article/details/81117842
今日推荐