常用String扩展(JavaScript)

来源:互联网 发布:iphone远程mac 编辑:程序博客网 时间:2024/06/07 11:58
/** * ------------------------------------------------------------- * 删除首尾空格 * 如果限定了长度的话, 则区分全/半角截短字符串, 2个ASCII字符按1个字符计数 * 截取后的显示长度不会超过限定长度, 省略号也计算在内 * ------------------------------------------------------------- * 如: " abc ".trim() --> "abc" * 如: "even偶数".trim(3) --> "<span title=\"even偶数\">even..</span>" * 如: "even偶数".trim(4) --> "even偶数" // 4个英文=2个汉字 * 如: "even偶数!".trim(4) --> "<span title=\"even偶数!\">even偶..</span>" * 如: "odd奇数".trim(2) --> "<span title="odd奇数">od..</span>" * ------------------------------------------------------------- * author: zhaohuihua * date: 2011-01-05 * ------------------------------------------------------------- */String.prototype.trim = function(len) {var s = this.replace(/(^\s+|\s+$)/g, "");if(len === undefined || isNaN(len = parseInt(len)))return s;var idx = 0, charlen = 0; len = (len - 1) * 2;for(var i = 0; i < s.length; i ++) {var code = s.charCodeAt(i);var ascii = code >= 0 && code <= 255;if((charlen += (ascii ? 1 : 2)) <= len) idx ++;}if(charlen <= len + 2) return s;else return "<span title=\"{0}\">{1}..</span>".format(s, s.substring(0,idx));};/** * 求字符串的长度, 1个汉字按2个字符计算 */String.prototype.size = function(len) {var count = 0;for(var i = 0; i < this.length; i ++) {var code = this.charCodeAt(i);count += (code >= 0 && code <= 255) ? 1 : 2;}return count;};/** * ------------------------------------------------------------- * 字符串格式化 * ------------------------------------------------------------- * 如: "Name:{0}, Email:{1}".format("zhaohuihua", "zhaohuihua@126.com") * --> Name:zhaohuihua, Email:zhaohuihua@126.com * ------------------------------------------------------------- * author: zhaohuihua * date: 2011-01-05 * ------------------------------------------------------------- */String.prototype.format = function() {var args = arguments;return this.replace(/\{(\d{1,2})\}/g, function(old, index) {var value = args[index]return value === undefined ? "" : value;});};/** * ------------------------------------------------------------- * 字符串加载JSON数据, 是字符串格式化的加强版 * -------------------------------------------------------------示例:var json = {    id:1001, name:"Avril Lavigne", extra:{download:1888, click:1999},    image:[{type:"pvw",path:"1001.1.jpg"},{type:"main",path:"1001.2.jpg"}],    music:["Runaway", "Innocence", "Contagious"]};"<li>{id}</li><li>{name.substring(0, 5)}</li>".load(json)--> <li>1001</li><li>Avril</li>"<li>{extra.click}</li><li>{image[1].path}</li>".load(json)--> <li>1999</li><li>1001.2.jpg</li>"<li>{music[0]}</li><li>{music[1]}</li><li>{music[2]}</li>".load(json)--> <li>Runaway</li><li>Innocence</li><li>Contagious</li> * ------------------------------------------------------------- * author: zhaohuihua * date: 2011-01-05 * ------------------------------------------------------------- */String.prototype.load = function(json) {// 首字母不能为数字, 以免与format()冲突var RPL = /\{([^0-9][^{}]*)\}/ig;return this.replace(RPL, function(old, exp) {try {var isArray = /\[[0-9]+\].*/.test(exp),value = eval("json" + (isArray ? "" : ".") + exp);return value === undefined ? "" : value;} catch (e) {return "";}});};/** * ------------------------------------------------------------- * 相当于oracle的decode函数 * ------------------------------------------------------------- * 如: "1".decode("1", "男", "2", "女", "未知") --> 男 * 如: "3".decode("1", "男", "2", "女", "未知") --> 未知 * ------------------------------------------------------------- * author: zhaohuihua * date: 2011-01-05 * ------------------------------------------------------------- */String.prototype.decode = function() {if(arguments.length < 2) return null; // 参数不够for(var i = 0; i < arguments.length;) {// 每两个为一组var key = arguments[i++], val = arguments[i++];if(this == key) return val;// 最后多出来的一个为默认值if(i + 1 == arguments.length) return arguments[i];}return null;};

0 0