zoukankan      html  css  js  c++  java
  • C# TrimStart,TrimEnd,Trim在javascript上的实现

        今天在后台写了个类,后来才发现,需要在JS上做..于是把代码拷到js上进行修改,代码中用到TrimStart,TrimEnd,Trim等方法,在网上找半天竟然没找到.要么就只能清除空格的!

        于是乎,自己动手写了个!!看到很多人都是用正则,咱不会,就用了最土的方法来实现了!帖上代码吧!希望对大家有所帮助!!!

    String.prototype.trimStart = function(trimStr){
    if(!trimStr){return this;}
    var temp = this;
    while(true){
    if(temp.substr(0,trimStr.length)!=trimStr){
    break;
    }
    temp
    = temp.substr(trimStr.length);
    }
    return temp;
    };
    String.prototype.trimEnd
    = function(trimStr){
    if(!trimStr){return this;}
    var temp = this;
    while(true){
    if(temp.substr(temp.length-trimStr.length,trimStr.length)!=trimStr){
    break;
    }
    temp
    = temp.substr(0,temp.length-trimStr.length);
    }
    return temp;
    };
    String.prototype.trim
    = function(trimStr){
    var temp = trimStr;
    if(!trimStr){temp=" ";}
    return this.trimStart(temp).trimEnd(temp);
    };

        用法大家应该明了吧!!!这里就不说了哈!!!有问题请指明!谢谢!

  • 相关阅读:
    【C语言天天练(二二)】位操作
    远程调用内核接口的封装类(RCKObjs)
    03010_防止SQL注入
    PHP 5 SimpleXML 函数
    PHP 5 String 函数
    PHP 5 MySQLi 函数
    PHP 杂项 函数
    PHP PDO
    PHP 5 时区
    分享海量 iOS 及 Mac 开源项目和学习资料
  • 原文地址:https://www.cnblogs.com/tao8825529/p/1936011.html
Copyright © 2011-2022 走看看