[js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展字符串位置方法(4)

本文,我们接着之前的框架继续扩展,这次扩展了一共有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
 
 ; (function (window, undefined) {
function init(obj, s) {
if (s !== null && s !== undefined) {
if (typeof s === 'string') {
obj.s = s;
} else {
obj.s = s.toString();
}
} else {
obj.s = s;
}
} function G(s) {
init(this, s);
} function GhostWu(s) {
return new G(s);
} var sProto = String.prototype;
G.prototype = {
constructor: G,
capitalize: function () {
return new this.constructor(this.s.slice(0, 1).toUpperCase() + this.s.substring(1).toLowerCase());
},
trimLeft: function () {
var s;
if (sProto.trimLeft === 'undefined')
s = this.s.trimLeft();
else
s = this.s.replace(/^\s+/g, '');
return new this.constructor(s);
},
trimRight: function () {
var s;
if (sProto.trimRight === 'undefined')
s = this.s.trimRight();
else
s = this.s.replace(/\s+$/g, '');
return new this.constructor(s);
},
trim: function () {
var s;
if (typeof sProto.trim === 'undefined') {
s = this.s.replace(/^\s+|\s+$/g, '');
} else {
s = this.s.trim();
}
return new this.constructor(s);
},
camelize: function () {
var s = this.trim().s.replace(/(\-|_|\s)+(.)?/g, function (s0, s1, s2) {
return (s2 ? s2.toUpperCase() : '');
});
return new this.constructor(s);
},
dasherize: function () {
var s = this.trim().s.replace(/[_\s]+/g, '-').replace(/([A-Z])/g, '-$1').replace(/-+/g, '-').toLowerCase();
return new this.constructor(s);
},
between: function (left, right) {
var s = this.s;
var startPos = s.indexOf(left);
var endPos = s.indexOf(right, startPos + left.length);
if (endPos == -1 && right != null)
return new this.constructor('')
else if (endPos == -1 && right == null)
return new this.constructor(s.substring(startPos + left.length))
else
return new this.constructor(s.slice(startPos + left.length, endPos));
},
chompLeft: function (prefix) {
var s = this.s;
if (s.indexOf(prefix) === 0) {
s = s.slice(prefix.length);
return new this.constructor(s);
} else {
return this;
}
},
startWith: function () {
var prefixes = [].slice.call(arguments, 0);
if (this.s.indexOf(prefixes[0], 0) === 0) return true;
return false;
},
endWith: function () {
var prefixes = [].slice.call(arguments, 0),
pos = 0;
while (pos !== -1) {
if (this.s.indexOf(prefixes[0], pos) === (this.s.length - prefixes[0].length)) return true;
pos = this.s.indexOf(prefixes[0], pos + prefixes[0].length);
}
return false;
},
chompRight: function (suffix) {
if (this.endWith(suffix)) {
var s = this.s;
s = s.slice(0, s.length - suffix.length);
return new this.constructor(s);
} else {
return this;
}
}
}; window.G = GhostWu;
})(window, undefined);
上一篇:解决com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭


下一篇:Find the k-th Smallest Element in the Union of Two Sorted Arrays