• JavaScript:字符串の空格刪減和字符刪減功能


    /* 刪除左右空格 */
    function trim(string){return string.replace(/(^s*)|(s*$)/g,'');};  //函數法
    String.prototype.__defineGetter__("trim",function(){return this.replace(/(^s*)|(s*$)/g,'')})  //prototype法
    
    /* 刪除左側空格 */
    function ltrim(string){return string.replace(/^s+/,'');};  //函數法
    String.prototype.__defineGetter__("ltrim",function(){return this.replace(/^s+/,'')})  //prototype法
    
    /* 刪除右側空格 */
    function rtrim(string){return string.replace(/s+$/,'');};  //函數法
    String.prototype.__defineGetter__("rtrim",function(){return this.replace(/s+$/,'')})  //prototype法
    
    /* 刪除所有空格 */
    function alltrim(string){return string.replace(/s/g,'');};  //函數法
    String.prototype.__defineGetter__("alltrim",function(){return this.replace(/s/g,'')})  //prototype法
    
    /* 刪除指定字符 */
    function delChar(string,char){var charReg=new RegExp(char,'ig');return string.replace(charReg,'');}
    
    //prototype法使用說明
    var a=" a b c d e ";
    console.log(a.ltrim);  //輸出"a b c d e "
    console.log(a.rtrim);  //輸出" a b c d e"
    console.log(a.trim);   //輸出"a b c d e"
    console.log(a.alltrim); //輸出"abcde"
  • 相关阅读:
    printcap
    browser-ua
    PHP 开发 APP 接口 学习笔记与总结
    Java实现 LeetCode 72 编辑距离
    Java实现 LeetCode 72 编辑距离
    Java实现 LeetCode 72 编辑距离
    Java实现 LeetCode 71 简化路径
    Java实现 LeetCode 71 简化路径
    Java实现 LeetCode 71 简化路径
    Java实现 LeetCode70 爬楼梯
  • 原文地址:https://www.cnblogs.com/mandongpiaoxue/p/10494626.html
Copyright © 2020-2023  润新知