• JS常用方法


    String.prototype用于为某字符串对象新增方法

    // 实现Contains方法(核心是用Index方法的返回值进行判断)

    String.prototype.Contains = function (str) { return (this.indexOf(str) > -1); };

    // 字符串替换

    String.prototype.Replace = function(oldValue,newValue) { var reg = new RegExp(oldValue,"g");  return this.replace(reg, newValue);}

    // 去掉字符两端的空白字符

    String.prototype.Trim = function (s) { if (s) return this.trimEnd(s).trimStart(s); else return this.replace(/(^[ ]*)|([ ]*$)/g, ''); };

    // 去掉字符结束的空白字符

    String.prototype.TrimEnd = function (s) { if (this.endsWith(s)) { return this.substring(0, this.length - s.length); } return this; };

    // 去掉字符开始的空白字符
    String.prototype.TrimStart = function (s) { if (this.startsWith(s)) { return this.slice(s.length); } return this; };

    // 判断字符串是否以指定的字符串开始
    String.prototype.StartsWith = function (str) { return (this.indexOf(str) == 0); };

    // 判断字符串是否以指定的字符串结束
    String.prototype.EndsWith = function (str) { return (str.length <= this.length && this.substr(this.length - str.length, str.length) == str); };
    String.prototype.Remove = function (start, l) { var str1 = this.substring(0, start); var str2 = this.substring(start + l, this.length); return str1 + str2; }
    String.prototype.Insert = function (index, str) { var str1 = this.substring(0, index); var str2 = this.substring(index, this.length); return str1 + str + str2; }
    String.prototype.GetHashCode = function () { var h = 31; var i = 0; var l = this.length; while (i < l) h ^= (h << 5) + (h >> 2) + this.charCodeAt(i++); return h; }

    // 返回字符的长度,一个中文算2个

    String.prototype.ChineseLength=function(){  return this.replace(/[^x00-xff]/g,"**").length;

    Array.prototype.contains = function (val) { for (var i = 0; i < this.length; i++) { if (val == this[i]) return true; } return false; }
    Date.prototype.Format = function (format) { var o = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() }; if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)) } for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)) } } return format }
    Date.prototype.AddDays = function (days) { var v = this.valueOf(); v += days * 24 * 60 * 60 * 1000; return new Date(v) }
    Date.prototype.Diff = function (date) { return new Date((this.getTime() - date.getTime()) / (24 * 60 * 60 * 1000)*1000) }

  • 相关阅读:
    MyEclipse编码集设置
    Tomcat内存溢出问题解决
    避免头文件多次编译
    C++指针学习(1)
    C++头文件和实现(用复数类举例)
    从helloworld开始
    标准库string类型
    浅谈Lua的Coroutine协程的多"线程"并发模型
    关于闭包函数的概念和原理
    笔记
  • 原文地址:https://www.cnblogs.com/raincedar/p/9869309.html
Copyright © 2020-2023  润新知