• JS 用 prototype 扩展实现string过滤空格


    JS的prototype是。。。。。。

    实现对字符串 头和尾 空格的过滤,代码如下所示:

        //Trim the head and tail spaces
        String.prototype.Trim = function () {
            return this.replace(/(^\s*)|(\s*$)/g, "");
        }

    实现对字符串 头(左侧Left) 空格的过滤, 代码如下所示:

        //Trim the head spaces of current string
        String.prototype.LTrim = function () {
            return this.replace(/(^\s*)/g, "");
        }

    实现对字符串 尾(右侧Right) 空格的过滤, 代码如下所示:

        //Trim the tail spaces of current string
        String.prototype.RTrim = function () {
            return this.replace(/(\s*$)/g, "");
        }

    实现Contains方法(核心是用Index方法的返回值进行判断),代码如下所示:

        //Judge current string contains substring or not
        String.prototype.Contains = function (subStr) {
            var currentIndex = this.indexOf(subStr);
            if (currentIndex != -1) {
                return true;
            }
            else {
                return false;
            }
        }

     。。。。。

  • 相关阅读:
    JS高级拖拽
    JS高级Date类
    JS高级闭包
    JS笔记整理
    JS高级解决函数内的this指向
    JS高级事件委托
    JS高级 事件对象
    JS中级面向对象
    JS中级
    JS高级
  • 原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/2871333.html
Copyright © 2020-2023  润新知