注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Computer Science

I Pressed My Words Here. Will You Read ?

 
 
 

日志

 
 

[JS]关于String的原生方法扩展  

2012-06-15 10:20:10|  分类: My Projects |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

移除字符串中第一次出现的指定子字符串(在实现文本格式缩进计算时用到)
if (!String.prototype.removeLastSubString) {
        String.prototype.removeLastSubString = function(word) {
                var lio = this.lastIndexOf(word);
                if (lio === - 1) {
                        return this.toString();
                }
                return (this.substring(0, lio) + this.substring((lio + word.length)));
        }
}



移除字符串中最后一次出现的指定子字符串(在实现文本格式缩进控制时用到)
if (!String.prototype.removeFirstSubString) {
        String.prototype.removeFirstSubString = function(word) {
                if (typeof word != 'string'{
                        return this
.toString();
                }
                var io = this.indexOf(word);
                if (io == - 1) {
                        return this;
                }
                return (this.substring(0, io) + this.substring((io + word.length)));
        }
}

将字符串重复指定次数并拼接(在实现文本缩进时用到)
if (!String.prototype.plus) {
        String.prototype.plus = function(num) {
                if (typeof num != 'number' || num == 'NaN'{
                        return this
.toString();
                }
                var str = '';
                while (num-- > 0) {
                        str += this;
                }
                return str;
        }
}

计算指定子字符串在字符串中出现的次数(实现文本缩进控制时用到)
if (!String.prototype.count) {
        String.prototype.count = function(str) {
                if (typeof str != 'string'{
                        return 0;
                };
                var s = this;
                var count = 0;
                var fromindex = 0;
                while(s.indexOf(str,fromindex) != -1){
                    count ++;
                    fromindex = s.indexOf(str,fromindex) + str.length;
                }
                return count;
                /*
                var s = this.match(new RegExp(str, "g"));
                if (s == null) {
                        return 0;
                }
                return s.length || 0;
                */
        }
}
对数组中的值进行本地化(人性化)排序(就是字母按照字母顺序,数字按照数字顺序,而非字符编码表的顺序)
if (!Array.prototype.sortLocale) {
        Array.prototype.sortLocale = function() {
                /**
          * 比较函数
          * @param {Object} param1 要比较的参数1
          * @param {Object} param2 要比较的参数2
          * @return {Number} 如果param1 > param2 返回 1
          *                     如果param1 == param2 返回 0
          *                     如果param1 < param2 返回 -1
          */
                var compareFunc = function(param1, param2) {
                        //如果两个参数均为字符串类型
                        if (typeof param1 == "string" && typeof param2 == "string"{
                                return param2.localeCompare(param1);
                        } else
                        //如果参数1为数字,参数2为字符串
                        if (typeof param1 == "number" && typeof param2 == "string"{
                                return 1;
                        } else
                        //如果参数1为字符串,参数2为数字
                        if (typeof param1 == "string" && typeof param2 == "number"{
                                return - 1;
                        } else
                        //如果两个参数均为数字
                        if (typeof param1 == "number" && typeof param2 == "number"{
                                if (param1 > param2) return - 1;
                                if (param1 == param2) return 0;
                                if (param1 < param2) return 1;
                        }
                }
                this.sort(compareFunc);
                return this;
        }
}
对数组中的值进行查找,并返回符合的第一个值的数组索引值。
if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(elt
        /*, from*/
        ) {
                var len = this.length;

                var from = Number(arguments[1]) || 0;
                from = (from < 0) ? Math.ceil(from) : Math.floor(from);
                if (from < 0) from += len;

                for (; from < len; from++) {
                        if (from in this && this[from] === elt) return from;
                }
                return - 1;
        }
}

对数组中的值进行查找,并返回符合的第一个值的数组索引值。
if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(elt
        /*, from*/
        ) {
                var len = this.length;

                var from = Number(arguments[1]) || 0;
                from = (from < 0) ? Math.ceil(from) : Math.floor(from);
                if (from < 0) from += len;

                for (; from < len; from++) {
                        if (from in this && this[from] === elt) return from;
                }
                return - 1;
        }
}

对数组中的值进行查找,并返回符合的第一个值的数组索引值。
if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(elt
        /*, from*/
        ) {
                var len = this.length;

                var from = Number(arguments[1]) || 0;
                from = (from < 0) ? Math.ceil(from) : Math.floor(from);
                if (from < 0) from += len;

                for (; from < len; from++) {
                        if (from in this && this[from] === elt) return from;
                }
                return - 1;
        }
}

对数组中的值进行查找,并返回符合的第一个值的数组索引值。
if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function(elt
        /*, from*/
        ) {
                var len = this.length;

                var from = Number(arguments[1]) || 0;
                from = (from < 0) ? Math.ceil(from) : Math.floor(from);
                if (from < 0) from += len;

                for (; from < len; from++) {
                        if (from in this && this[from] === elt) return from;
                }
                return - 1;
        }
}
  评论这张
 
阅读(707)| 评论(2)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017