[js高手之路] 跟GhostWu一起封装一个字符串工具…

2018-06-24 00:00:59来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

本文,我们接着之前的框架继续扩展,这次扩展了一共有5个与字符串位置相关的方法

between( left, right )

返回两个字符串之间的内容, 如果第二个参数没有传递,返回的是找到的第一个参数 之后 到 字符串结尾的所有字符串

如果第二个参数传递了,但是从left这个位置查找不到,就返回空字符串

例子:

G( 'ghost wu tell you how to learn js' ).between( 'tell', 'learn' ).s   
返回的是tell和learn之间的字符串, 结果为:you how to
G( 'ghost wu tell you how to learn js' ).between( 'tell' ).s
返回的是tell后面所有的字符串,结果为:you how to learn js
G( 'ghost wu tell you how to learn js' ).between( 'tell', 'wu' ).s
返回空字符串
 
chompLeft( prefix )
例子:返回以prefix开头 到 字符串结尾的所有字符串
G( 'ghost wu tell you how to learn js' ).chompLeft( 'tell' ).s; //ghost wu tell you how to learn js
G( 'ghost wu tell you how to learn js' ).chompLeft( 'ghost' ).s; //wu tell you how to learn js
 
startWith( prefix ): 判断是否以prefix这个字符串开始
endWith( prefix ): 判断是否以prefixe这个字符串结尾
G( 'ghost wu tell you how to learn js---ghost wu' ).startWith('wu');//false
G( 'ghost wu!asbc# ghost wu' ).endWith('wu');//true
 
chompRight( prefix )
例子:返回从字符串开始到以prefix结尾之间的字符串
G( 'ghost wu tell you how to learn js---ghost wu' ).chompRight('wu').s;//ghost wu tell you how to learn js---ghost
 
  1 ; (function (window, undefined) {
  2     function init(obj, s) {
  3         if (s !== null && s !== undefined) {
  4             if (typeof s === 'string') {
  5                 obj.s = s;
  6             } else {
  7                 obj.s = s.toString();
  8             }
  9         } else {
 10             obj.s = s;
 11         }
 12     }
 13 
 14     function G(s) {
 15         init(this, s);
 16     }
 17 
 18     function GhostWu(s) {
 19         return new G(s);
 20     }
 21 
 22     var sProto = String.prototype;
 23     G.prototype = {
 24         constructor: G,
 25         capitalize: function () {
 26             return new this.constructor(this.s.slice(0, 1).toUpperCase() + this.s.substring(1).toLowerCase());
 27         },
 28         trimLeft: function () {
 29             var s;
 30             if (sProto.trimLeft === 'undefined')
 31                 s = this.s.trimLeft();
 32             else
 33                 s = this.s.replace(/^\s+/g, '');
 34             return new this.constructor(s);
 35         },
 36         trimRight: function () {
 37             var s;
 38             if (sProto.trimRight === 'undefined')
 39                 s = this.s.trimRight();
 40             else
 41                 s = this.s.replace(/\s+$/g, '');
 42             return new this.constructor(s);
 43         },
 44         trim: function () {
 45             var s;
 46             if (typeof sProto.trim === 'undefined') {
 47                 s = this.s.replace(/^\s+|\s+$/g, '');
 48             } else {
 49                 s = this.s.trim();
 50             }
 51             return new this.constructor(s);
 52         },
 53         camelize: function () {
 54             var s = this.trim().s.replace(/(\-|_|\s)+(.)?/g, function (s0, s1, s2) {
 55                 return (s2 ? s2.toUpperCase() : '');
 56             });
 57             return new this.constructor(s);
 58         },
 59         dasherize: function () {
 60             var s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase();
 61             return new this.constructor(s);
 62         },
 63         between: function (left, right) {
 64             var s = this.s;
 65             var startPos = s.indexOf(left);
 66             var endPos = s.indexOf(right, startPos + left.length);
 67             if (endPos == -1 && right != null)
 68                 return new this.constructor('')
 69             else if (endPos == -1 && right == null)
 70                 return new this.constructor(s.substring(startPos + left.length))
 71             else
 72                 return new this.constructor(s.slice(startPos + left.length, endPos));
 73         },
 74         chompLeft: function (prefix) {
 75             var s = this.s;
 76             if (s.indexOf(prefix) === 0) {
 77                 s = s.slice(prefix.length);
 78                 return new this.constructor(s);
 79             } else {
 80                 return this;
 81             }
 82         },
 83         startWith: function () {
 84             var prefixes = [].slice.call(arguments, 0);
 85             if (this.s.indexOf(prefixes[0], 0) === 0) return true;
 86             return false;
 87         },
 88         endWith: function () {
 89             var prefixes = [].slice.call(arguments, 0),
 90                 pos = 0;
 91             while (pos !== -1) {
 92                 if (this.s.indexOf(prefixes[0], pos) === (this.s.length - prefixes[0].length)) return true;
 93                 pos = this.s.indexOf(prefixes[0], pos + prefixes[0].length);
 94             }
 95             return false;
 96         },
 97         chompRight: function (suffix) {
 98             if (this.endWith(suffix)) {
 99                 var s = this.s;
100                 s = s.slice(0, s.length - suffix.length);
101                 return new this.constructor(s);
102             } else {
103                 return this;
104             }
105         }
106     };
107 
108     window.G = GhostWu;
109 })(window, undefined);

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:浅谈javascript的面向对象思想

下一篇:开启AngularJS之路